package com.jeeplus.test.wdt.merchantCompany.service;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.jeeplus.core.query.QueryWrapperGenerator;
import com.jeeplus.test.wdt.merchantCompany.domain.MerchantCompany;
import com.jeeplus.test.wdt.merchantCompany.mapper.MerchantCompanyMapper;
import com.jeeplus.test.wdt.merchantCompany.service.dto.MerchantCompanyDTO;
import com.jeeplus.test.wdt.merchantCompany.service.dto.MerchantCompanyScreenDTO;
import com.jeeplus.test.wdt.merchantCompany.service.dto.MerchantProductDTO;
import com.jeeplus.test.wdt.merchantCompany.service.dto.SettledPlatformDTO;
import com.jeeplus.test.wdt.merchantCompany.service.mapstuct.MerchantCompanyWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.apache.commons.lang3.StringUtils;

import java.util.List;

/**
 * 入驻商家service
 * @author: 徐滕
 * @version: 2024-01-04 10:56
 */
@Service
@Transactional
public class MerchantCompanyService extends ServiceImpl<MerchantCompanyMapper, MerchantCompany> {

    @Autowired
    private MerchantCompanyWrapper merchantCompanyWrapper;

    /**
     * 自定义分页检索
     * @param page
     * @param merchantCompanyDTO
     * @return
     */
    public IPage<MerchantCompanyDTO> findPage(Page<MerchantCompanyDTO> page, MerchantCompanyDTO merchantCompanyDTO) throws Exception {
        QueryWrapper<MerchantCompany> queryWrapper = QueryWrapperGenerator.buildQueryCondition ( MerchantCompanyWrapper.INSTANCE.toEntity(merchantCompanyDTO), MerchantCompany.class );

        queryWrapper.eq ("a.del_flag", 0 ); // 排除已经删除
        queryWrapper.orderByDesc("a.update_time");
        if(StringUtils.isNotBlank(merchantCompanyDTO.getCompanyName())){
            queryWrapper.like("a.company_name",merchantCompanyDTO.getCompanyName());
        }
        if(StringUtils.isNotBlank(merchantCompanyDTO.getLinkmanName())){
            queryWrapper.like("a.linkman_name",merchantCompanyDTO.getLinkmanName());
        }
        if(StringUtils.isNotBlank(merchantCompanyDTO.getLinkmanPhone())){
            queryWrapper.like("a.linkman_phone",merchantCompanyDTO.getLinkmanPhone());
        }
        if (merchantCompanyDTO.getAnnualSalesList() != null && merchantCompanyDTO.getAnnualSalesList().length > 0) {
            if(merchantCompanyDTO.getAnnualSalesList().length == 1){
                queryWrapper.ge("a.annual_sales",merchantCompanyDTO.getAnnualSalesList()[0]);
            }else{
                queryWrapper.between("a.annual_sales", merchantCompanyDTO.getAnnualSalesList()[0], merchantCompanyDTO.getAnnualSalesList()[1]);
            }
        }
        if (merchantCompanyDTO.getOnlineRetailSalesList() != null && merchantCompanyDTO.getOnlineRetailSalesList().length > 0) {
            if(merchantCompanyDTO.getOnlineRetailSalesList().length == 1){
                queryWrapper.ge("a.online_retail_sales",merchantCompanyDTO.getOnlineRetailSalesList()[0]);
            }else {
                queryWrapper.between("a.online_retail_sales", merchantCompanyDTO.getOnlineRetailSalesList()[0], merchantCompanyDTO.getOnlineRetailSalesList()[1]);
            }
        }
        if (merchantCompanyDTO.getDates() != null && merchantCompanyDTO.getDates().length > 0) {
            if(merchantCompanyDTO.getDates().length == 1){
                queryWrapper.ge("a.create_time",merchantCompanyDTO.getDates()[0]);
            }else {
                queryWrapper.between("a.create_time", merchantCompanyDTO.getDates()[0], merchantCompanyDTO.getDates()[1]);
            }
        }
        if(StringUtils.isNotBlank(merchantCompanyDTO.getSettledPlatformId())){
            queryWrapper.like("mcori.occupancy_id",merchantCompanyDTO.getSettledPlatformId());
        }
        return  baseMapper.findList (page, queryWrapper);
    }
    /**
     * 自定义分页检索
     * @param page
     * @param merchantCompanyDTO
     * @return
     */
    public List<MerchantCompanyScreenDTO> getListJson(MerchantCompanyDTO merchantCompanyDTO) throws Exception {
        QueryWrapper<MerchantCompany> queryWrapper = QueryWrapperGenerator.buildQueryCondition ( MerchantCompanyWrapper.INSTANCE.toEntity(merchantCompanyDTO), MerchantCompany.class );

        queryWrapper.eq ("a.del_flag", 0 ); // 排除已经删除
        queryWrapper.orderByDesc("a.update_time");
        if(StringUtils.isNotBlank(merchantCompanyDTO.getCompanyName())){
            queryWrapper.like("a.company_name",merchantCompanyDTO.getCompanyName());
        }
        if(StringUtils.isNotBlank(merchantCompanyDTO.getLinkmanName())){
            queryWrapper.like("a.linkman_name",merchantCompanyDTO.getLinkmanName());
        }
        if(StringUtils.isNotBlank(merchantCompanyDTO.getLinkmanPhone())){
            queryWrapper.like("a.linkman_phone",merchantCompanyDTO.getLinkmanPhone());
        }
        if(StringUtils.isNotBlank(merchantCompanyDTO.getCompanyType())){
            queryWrapper.eq("mcrt.type_id",merchantCompanyDTO.getCompanyType());
        }
        if (merchantCompanyDTO.getAnnualSalesList() != null && merchantCompanyDTO.getAnnualSalesList().length > 0) {
            if(merchantCompanyDTO.getAnnualSalesList().length == 1){
                queryWrapper.ge("a.annual_sales",merchantCompanyDTO.getAnnualSalesList()[0]);
            }else{
                queryWrapper.between("a.annual_sales", merchantCompanyDTO.getAnnualSalesList()[0], merchantCompanyDTO.getAnnualSalesList()[1]);
            }
        }
        if (merchantCompanyDTO.getOnlineRetailSalesList() != null && merchantCompanyDTO.getOnlineRetailSalesList().length > 0) {
            if(merchantCompanyDTO.getOnlineRetailSalesList().length == 1){
                queryWrapper.ge("a.online_retail_sales",merchantCompanyDTO.getOnlineRetailSalesList()[0]);
            }else {
                queryWrapper.between("a.online_retail_sales", merchantCompanyDTO.getOnlineRetailSalesList()[0], merchantCompanyDTO.getOnlineRetailSalesList()[1]);
            }
        }
        if (merchantCompanyDTO.getDates() != null && merchantCompanyDTO.getDates().length > 0) {
            if(merchantCompanyDTO.getDates().length == 1){
                queryWrapper.ge("a.create_time",merchantCompanyDTO.getDates()[0]);
            }else {
                queryWrapper.between("a.create_time", merchantCompanyDTO.getDates()[0], merchantCompanyDTO.getDates()[1]);
            }
        }
        if(StringUtils.isNotBlank(merchantCompanyDTO.getSettledPlatformId())){
            queryWrapper.like("mcori.occupancy_id",merchantCompanyDTO.getSettledPlatformId());
        }
        List<MerchantCompanyDTO> listJson = baseMapper.getListJson(queryWrapper);

        List<MerchantCompanyScreenDTO> result = Lists.newArrayList();
        for (MerchantCompanyDTO companyDTO : listJson) {
            MerchantCompanyScreenDTO info = new MerchantCompanyScreenDTO();
            info.setAnnualSales(companyDTO.getAnnualSales());
            info.setCompanyName(companyDTO.getCompanyName());
            info.setLinkmanName(companyDTO.getLinkmanName());
            info.setLinkmanPhone(companyDTO.getLinkmanPhone());
            result.add(info);
        }
        return result;
    }


    /**
     * 根据id查询
     * @param id
     * @return
     */
    public MerchantCompanyDTO findById(String id) {
        return baseMapper.findById ( id );
    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    public MerchantCompanyDTO getByCompanyName(String companyName) {
        return baseMapper.getByCompanyName ( companyName );
    }




    /**
     * 保存或者更新商户信息
     *
     * @param merchantCompanyDTO
     */
    public void saveOrUpdate(MerchantCompanyDTO merchantCompanyDTO) {
        //处理是否为电商小铺
        if(StringUtils.isNotBlank(merchantCompanyDTO.getCommerceShop()) && !"1".equals(merchantCompanyDTO.getCommerceShop()) && !"0".equals(merchantCompanyDTO.getCommerceShop())){
            switch (merchantCompanyDTO.getCommerceShop()){
                case "是":
                    merchantCompanyDTO.setCommerceShop("1");
                    break;
                case "否":
                    merchantCompanyDTO.setCommerceShop("0");
                    break;
                default:
                    merchantCompanyDTO.setCommerceShop("");
                    break;
            }
        }
        //处理公司类型
        if(StringUtils.isNotBlank(merchantCompanyDTO.getCompanyType()) && !"1".equals(merchantCompanyDTO.getCompanyType()) && !"2".equals(merchantCompanyDTO.getCompanyType())){
            switch (merchantCompanyDTO.getCompanyType()){
                case "产品加工企业":
                    merchantCompanyDTO.setCompanyType("1");
                    break;
                case "合作企业":
                    merchantCompanyDTO.setCompanyType("2");
                    break;
                default:
                    merchantCompanyDTO.setCompanyType("");
                    break;
            }
        }
        MerchantCompany merchantCompany = merchantCompanyWrapper.toEntity ( merchantCompanyDTO );
        super.saveOrUpdate ( merchantCompany );
        if ( StrUtil.isBlank ( merchantCompanyDTO.getId ( ) ) ) {
            merchantCompanyDTO.setId ( merchantCompany.getId ( ) );
        }
        // 更新商户与入驻平台关系
        baseMapper.deleteSettledPlatform ( merchantCompanyDTO.getId ( ) );
        if ( merchantCompanyDTO.getSettledPlatformDTOList ( ) != null ) {
            merchantCompanyDTO.getSettledPlatformDTOList ( ).forEach ( settledPlatformDTO -> {
                baseMapper.insertSettledPlatform ( merchantCompanyDTO.getId ( ), settledPlatformDTO.getId ( ) );
            } );
        } else {
            throw new RuntimeException ( merchantCompanyDTO.getCompanyName() + "没有入驻平台！" );
        }
        // 更新商户与入驻产品关系
        baseMapper.deleteMerchantProduct ( merchantCompanyDTO.getId ( ) );
        if ( merchantCompanyDTO.getMerchantProductDTOList ( ) != null ) {
            merchantCompanyDTO.getMerchantProductDTOList ( ).forEach ( merchantProductDTO -> {
                baseMapper.insertMerchantProduct ( merchantCompanyDTO.getId ( ), merchantProductDTO.getId ( ) );
            } );
        } else {
            throw new RuntimeException ( merchantCompanyDTO.getCompanyName() + "没有入驻产品！" );
        }
        // 更新商户与入驻公司类型关系
        baseMapper.deleteMerchantCompanyType ( merchantCompanyDTO.getId ( ) );
        if ( merchantCompanyDTO.getCompanyTypeDTOList ( ) != null ) {
            merchantCompanyDTO.getCompanyTypeDTOList ( ).forEach ( companyTypeDTO -> {
                baseMapper.insertMerchantCompanyType ( merchantCompanyDTO.getId ( ), companyTypeDTO.getId ( ) );
            } );
        } else {
            throw new RuntimeException ( merchantCompanyDTO.getCompanyName() + "没有入驻公司类型信息！" );
        }
    }

}
