package com.iotechn.unimall.admin.api.sys;


import com.iotechn.unimall.core.exception.AdminServiceException;
import com.iotechn.unimall.core.exception.ExceptionDefinition;
import com.iotechn.unimall.core.exception.ServiceException;
import com.iotechn.unimall.data.domain.CustomerBillingDetailDO;
import com.iotechn.unimall.data.dto.CollectionPaymentDTO;
import com.iotechn.unimall.data.dto.CollectionPaymentTiDTO;
import com.iotechn.unimall.data.dto.CustomerBillingDetailDTO;
import com.iotechn.unimall.data.mapper.CustomerBillingDetailMapper;
import com.iotechn.unimall.data.model.Page;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.iotechn.unimall.data.domain.CustomerBillingInfoDO;
import com.iotechn.unimall.data.dto.CustomerBillingInfoDTO;
import com.iotechn.unimall.data.mapper.CustomerBillingInfoMapper;
import com.iotechn.unimall.data.util.ExcelUtil;
import jodd.util.CollectionUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.BeanUtils;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.util.StringUtil;

import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Generate Code By Unimall
 */
@Service
public class CustomerBillingServiceImpl implements CustomerBillingService {

    @Autowired
    private CustomerBillingInfoMapper customerBillingInfoMapper;
    @Autowired
    private CustomerBillingDetailMapper customerBillingDetailMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean delete(Long id, Long adminId) throws ServiceException {
        customerBillingDetailMapper.delete(new EntityWrapper<CustomerBillingDetailDO>()
                .eq("info_id",id));
        return customerBillingInfoMapper.deleteById(id) > 0;
    }

    @Override
    public Page<CustomerBillingInfoDO> list(CustomerBillingInfoDTO selectDTO, Integer page, Integer limit, Long adminId) throws ServiceException {
        Wrapper<CustomerBillingInfoDO> wrapper = new EntityWrapper<CustomerBillingInfoDO>();
        if (!StringUtils.isEmpty(selectDTO.getPayNo())) {
            wrapper.like("pay_no", selectDTO.getPayNo());
        }
        if (!StringUtils.isEmpty(selectDTO.getBusinessType())) {
            wrapper.eq("business_type", selectDTO.getBusinessType());
        }
        if (!StringUtils.isEmpty(selectDTO.getStatus())) {
            wrapper.eq("status", selectDTO.getStatus());
        }
        if (StringUtil.isNotEmpty(selectDTO.getStartDate())) {
            wrapper.and("DATE_FORMAT(closing_date,'%Y%m%d') >= DATE_FORMAT('" + selectDTO.getStartDate() + "','%Y%m%d')");
        }
        if (StringUtil.isNotEmpty(selectDTO.getEndDate())) {
            wrapper.and("DATE_FORMAT(closing_date,'%Y%m%d') <= DATE_FORMAT('" + selectDTO.getEndDate() + "','%Y%m%d')");

        }
        List<CustomerBillingInfoDO> list = customerBillingInfoMapper.selectPage(new RowBounds((page - 1) * limit, limit), wrapper);
        Integer count = customerBillingInfoMapper.selectCount(wrapper);
        return new Page<CustomerBillingInfoDO>(list, page, limit, count);
    }


    @Override
    public List<String> customerList() throws ServiceException {
        Wrapper<CustomerBillingInfoDO> wrapper = new EntityWrapper<CustomerBillingInfoDO>();
            wrapper.groupBy("customer");
        List<CustomerBillingInfoDO> list = customerBillingInfoMapper.selectList(wrapper);
        List<String> customer = list.stream().map(CustomerBillingInfoDO::getCustomer).collect(Collectors.toList());
        return customer;
    }
    @Override
    public List<CustomerBillingInfoDO> cusDate(String customer) throws ServiceException {
        Wrapper<CustomerBillingInfoDO> wrapper = new EntityWrapper<CustomerBillingInfoDO>();
        wrapper.eq("customer",customer);
        List<CustomerBillingInfoDO> list = customerBillingInfoMapper.selectList(wrapper);
        for(int i=0;i<list.size();i++){
            List<CustomerBillingDetailDO> customerBillingDetailDOS = customerBillingDetailMapper.selectList(new EntityWrapper<CustomerBillingDetailDO>()
                    .eq("info_id", list.get(i).getId()));
            list.get(i).setCustomerBillingDetailDOList(customerBillingDetailDOS);
        }
        return list;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public CustomerBillingInfoDO create(CustomerBillingInfoDTO insertDTO, Long adminId) throws ServiceException {
        Date now = new Date();
        CustomerBillingInfoDO insertDO = new CustomerBillingInfoDO();
        BeanUtils.copyProperties(insertDTO, insertDO);
        insertDO.setGmtUpdate(now);
        insertDO.setGmtCreate(now);
        if (customerBillingInfoMapper.insert(insertDO) > 0) {
            List<CustomerBillingDetailDTO> customerBillingDetailDTOS = insertDTO.getCustomerBillingDetailDTOS();
            if (!CollectionUtils.isEmpty(customerBillingDetailDTOS)) {
                for (int i = 0; i < customerBillingDetailDTOS.size(); i++) {
                    CustomerBillingDetailDTO customerBillingDetailDTO = customerBillingDetailDTOS.get(i);
                    CustomerBillingDetailDO customerBillingDetailDO = new CustomerBillingDetailDO();
                    BeanUtils.copyProperties(customerBillingDetailDTO, customerBillingDetailDO);
                    customerBillingDetailDO.setInfoId(insertDO.getId());
                    customerBillingDetailDO.setGmtUpdate(now);
                    customerBillingDetailDO.setGmtCreate(now);
                    customerBillingDetailMapper.insert(customerBillingDetailDO);
                }

            }
            return insertDO;
        }
        throw new AdminServiceException(ExceptionDefinition.ADMIN_UNKNOWN_EXCEPTION);
    }

    @Override
    public CustomerBillingInfoDO getInfo(Long id) throws ServiceException {
        CustomerBillingInfoDO customerBillingInfoDO = customerBillingInfoMapper.selectById(id);
        if (customerBillingInfoDO != null) {
            List<CustomerBillingDetailDO> customerBillingDetailDOS = customerBillingDetailMapper.selectList(new EntityWrapper<CustomerBillingDetailDO>()
                    .eq("info_id", id));
            customerBillingInfoDO.setCustomerBillingDetailDOList(customerBillingDetailDOS);
        }
        return customerBillingInfoDO;
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public String edit(CustomerBillingInfoDTO editDTO, Long adminId) throws ServiceException {
        Date now = new Date();
        CustomerBillingInfoDO updateDO = new CustomerBillingInfoDO();
        BeanUtils.copyProperties(editDTO, updateDO);
        updateDO.setGmtUpdate(now);
        if (customerBillingInfoMapper.updateById(updateDO) > 0) {
            customerBillingDetailMapper.delete(new EntityWrapper<CustomerBillingDetailDO>()
            .eq("info_id",editDTO.getId()));
            List<CustomerBillingDetailDTO> customerBillingDetailDTOS = editDTO.getCustomerBillingDetailDTOS();
            if (!CollectionUtils.isEmpty(customerBillingDetailDTOS)) {
                for (int i = 0; i < customerBillingDetailDTOS.size(); i++) {
                    CustomerBillingDetailDTO customerBillingDetailDTO = customerBillingDetailDTOS.get(i);
                    CustomerBillingDetailDO customerBillingDetailDO = new CustomerBillingDetailDO();
                    BeanUtils.copyProperties(customerBillingDetailDTO, customerBillingDetailDO);
                    customerBillingDetailDO.setGmtUpdate(now);
                    customerBillingDetailDO.setGmtCreate(now);
                    customerBillingDetailMapper.insert(customerBillingDetailDO);
                }

            }

            return "ok";
        }
        throw new AdminServiceException(ExceptionDefinition.ADMIN_UNKNOWN_EXCEPTION);
    }


    @Override
    public String export(CustomerBillingInfoDTO customerBillingInfoDTO, HttpServletResponse response) throws Exception {

            ExcelUtil<CustomerBillingInfoDTO> util = new ExcelUtil<CustomerBillingInfoDTO>(CustomerBillingInfoDTO.class);
            return util.exportExcel(customerBillingInfoDTO.getCustomerBillingInfoDTOS(), "客户账单信息");

    }
    @Override
    public String exportDetail(CustomerBillingDetailDTO customerBillingDetailDTO, HttpServletResponse response) throws Exception {

        ExcelUtil<CustomerBillingDetailDTO> util = new ExcelUtil<CustomerBillingDetailDTO>(CustomerBillingDetailDTO.class);
        return util.exportExcel(customerBillingDetailDTO.getCustomerBillingDetailDTOList(), "客户账单详情信息");

    }


}
