package com.usky.iot.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.usky.common.core.bean.CommonPage;
import com.usky.common.security.utils.SecurityUtils;
import com.usky.iot.domain.CrmCustomInfo;
import com.usky.iot.domain.CrmInvoiceInfo;
import com.usky.iot.mapper.CrmInvoiceInfoMapper;
import com.usky.iot.service.CrmCustomInfoService;
import com.usky.iot.service.CrmInvoiceInfoService;
import com.usky.common.mybatis.core.AbstractCrudService;
import com.usky.iot.service.vo.CrmInvoiceInfoExportVO;
import com.usky.iot.service.vo.CrmInvoiceInfoRequest;
import com.usky.iot.service.vo.CrmInvoiceInfoStatisticVO;
import com.usky.iot.service.vo.CrmInvoiceInfoVO;
import org.apache.commons.lang.time.DateFormatUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * <p>
 * 客户发票信息表 服务实现类
 * </p>
 *
 * @author han
 * @since 2023-06-29
 */
@Service
public class CrmInvoiceInfoServiceImpl extends AbstractCrudService<CrmInvoiceInfoMapper, CrmInvoiceInfo> implements CrmInvoiceInfoService {
    @Autowired
    private CrmCustomInfoService crmCustomInfoService;

    @Override
    public void add(CrmInvoiceInfo crmInvoiceInfo){

        crmInvoiceInfo.setCreateBy(SecurityUtils.getUsername());
        crmInvoiceInfo.setCreateTime(LocalDateTime.now());
        crmInvoiceInfo.setTenantId(SecurityUtils.getTenantId());
        crmInvoiceInfo.setInvoiceStatus(1);
        this.save(crmInvoiceInfo);
    }

    @Override
    public void update(CrmInvoiceInfo invoiceInfo){
        this.updateById(invoiceInfo);
    }

    @Override
    public CommonPage<CrmInvoiceInfo> page(CrmInvoiceInfoRequest crmInvoiceInfoRequest){
        IPage<CrmInvoiceInfo> page = new Page<>(crmInvoiceInfoRequest.getCurrent(),crmInvoiceInfoRequest.getSize());
        page = baseMapper.page(page,crmInvoiceInfoRequest);
        return new CommonPage<>(page.getRecords(),page.getTotal(),page.getSize(),page.getCurrent());
    }

    @Override
    public List<CrmInvoiceInfoStatisticVO> statistic(CrmInvoiceInfoRequest crmInvoiceInfoRequest){
        List<CrmInvoiceInfoStatisticVO> list = new ArrayList<>();
        QueryWrapper<CrmInvoiceInfo> queryWrapper = Wrappers.query();
        queryWrapper.select("count(*) as invoiceApplicantNum","SUM(CASE WHEN invoice_status=1 THEN amount END) as notInvoicedAmount","SUM(invoice_status=1) as notInvoicedNum","SUM(invoice_status=2) as invoicedNum")
                .between("create_time",crmInvoiceInfoRequest.getStartTime(),crmInvoiceInfoRequest.getEndTime());
        List<Map<String,Object>> maps = this.listMaps(queryWrapper);
        if(maps.size() > 0){
            for (int i = 0; i < maps.size(); i++) {
                CrmInvoiceInfoStatisticVO statisticVO = new CrmInvoiceInfoStatisticVO();
                statisticVO.setInvoiceApplicantNum(Integer.parseInt(maps.get(i).get("invoiceApplicantNum").toString()));

                if(Objects.nonNull(maps.get(i).get("notInvoicedAmount"))){
                    statisticVO.setNotInvoicedAmount(Double.parseDouble(maps.get(i).get("notInvoicedAmount").toString()));
                }else{
                    statisticVO.setNotInvoicedAmount(Double.parseDouble("0"));
                }
                if(Objects.nonNull(maps.get(i).get("notInvoicedNum"))){
                    statisticVO.setNotInvoicedNum(Integer.parseInt(maps.get(i).get("notInvoicedNum").toString()));
                }else{
                    statisticVO.setNotInvoicedNum(0);
                }
                if(Objects.nonNull(maps.get(i).get("invoicedNum"))){
                    statisticVO.setInvoicedNum(Integer.parseInt(maps.get(i).get("invoicedNum").toString()));
                }else{
                    statisticVO.setInvoicedNum(0);
                }

                list.add(statisticVO);
            }
        }

        return list;
    }

    @Override
    public List<CrmInvoiceInfoExportVO> export(CrmInvoiceInfoRequest crmInvoiceInfoRequest){
        List<CrmInvoiceInfoExportVO> list = new ArrayList<>();

        List<CrmInvoiceInfo> list1 = baseMapper.export(crmInvoiceInfoRequest);
        if(CollectionUtils.isNotEmpty(list1)){
            for(int i=0;i<list1.size();i++){
                CrmInvoiceInfoExportVO exportVO = new CrmInvoiceInfoExportVO();
                exportVO.setProjectId(list1.get(i).getProjectId());
                exportVO.setInvoiceTitle(list1.get(i).getInvoiceTitle());
                exportVO.setDutyId(list1.get(i).getDutyId());
                exportVO.setInvoiceAttribute(list1.get(i).getInvoiceAttribute());  //发票性质;1：电子发票,2：纸质发票
                exportVO.setInvoiceType(list1.get(i).getInvoiceType()); //发票类型;1：普票,2：专票
                exportVO.setAmount(list1.get(i).getAmount().toString());
                exportVO.setApplicant(list1.get(i).getApplicant());
                exportVO.setPhone(list1.get(i).getPhone());
                exportVO.setEmail(list1.get(i).getEmail());
                exportVO.setSendAddress(list1.get(i).getSendAddress());
                exportVO.setPaymentReceipt(list1.get(i).getPaymentReceipt());
                exportVO.setCreateTime(list1.get(i).getCreateTime().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
                exportVO.setInvoiceStatus(list1.get(i).getInvoiceStatus());  //开票状态;1：未开票,2：已开票

                list.add(exportVO);
            }
        }
        return list;
    }

}
