package com.yiqi.core.service.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageInfo;
import com.yiqi.common.constant.HttpStatus;
import com.yiqi.common.core.page.TableDataInfo;
import com.yiqi.common.exception.ServiceException;
import com.yiqi.common.enums.ClothPriceType;
import com.yiqi.common.enums.ClothTypes;
import com.yiqi.common.enums.ClothWashModeType;
import com.yiqi.common.exception.ServiceException;
import com.yiqi.common.utils.DateUtils;
import com.yiqi.common.utils.SecurityUtils;
import com.yiqi.common.utils.StringUtils;
import com.yiqi.core.domain.ClothItem;
import com.yiqi.core.domain.ClothType;
import com.yiqi.core.domain.ClothWashMode;
import com.yiqi.core.domain.dto.ClothPriceImportDTO;
import com.yiqi.core.domain.dto.ClothPriceQueryByClothTypeDTO;
import com.yiqi.core.domain.dto.ClothPriceQueryByWashModeDTO;
import com.yiqi.core.domain.vo.*;
import com.yiqi.core.service.IClothItemService;
import com.yiqi.core.service.IClothTypeService;
import com.yiqi.core.service.IClothWashModeService;
import com.yiqi.recharge.domain.dto.RechargePasswordImportDTO;
import com.yiqi.system.domain.SysArea;
import com.yiqi.system.domain.SysStoreArea;
import com.yiqi.system.service.ISysStoreAreaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.yiqi.core.mapper.ClothPriceMapper;
import com.yiqi.core.domain.ClothPrice;
import com.yiqi.core.service.IClothPriceService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

/**
 * 衣服价格Service业务层处理
 *
 * @author xiaohei
 * @date 2023-08-22
 */
@Service
public class ClothPriceServiceImpl extends ServiceImpl<ClothPriceMapper, ClothPrice> implements IClothPriceService {

    @Autowired
    private IClothTypeService clothTypeService;

    @Autowired
    private IClothItemService clothItemService;

    @Autowired
    private ISysStoreAreaService sysStoreAreaService;

    @Autowired
    private IClothWashModeService clothWashModeService;

    @Override
    public List<ClothPriceQueryByClothTypeVO> getClothPriceListByClothType(ClothPriceQueryByClothTypeDTO clothPriceQueryByClothTypeDTO) {
        return baseMapper.getClothPriceListByClothType(clothPriceQueryByClothTypeDTO);
    }

    @Override
    public TableDataInfo getClothPriceListByClothTypeV2(ClothPriceQueryByClothTypeDTO clothPriceQueryByClothTypeDTO) {
        ClothItem clothItem = new ClothItem();
        clothItem.setName(clothPriceQueryByClothTypeDTO.getName());
        List<ClothItemVo> selectClothItemList = clothItemService.selectClothItemList(clothItem);
        SysStoreArea sysStoreArea = sysStoreAreaService.selectSysStoreAreaById(clothPriceQueryByClothTypeDTO.getStoreAreaId());
        List<ClothPriceQueryByClothTypeVO> clothPriceQueryByClothTypeVOS = new ArrayList<>();
        for (ClothItemVo clothItemVo : selectClothItemList) {
            ClothPriceQueryByClothTypeVO clothPriceQueryByClothTypeVO = new ClothPriceQueryByClothTypeVO();
            clothPriceQueryByClothTypeVO.setTypeName(ClothTypes.getInfoByCode(Integer.parseInt(clothPriceQueryByClothTypeDTO.getClothType())));
            clothPriceQueryByClothTypeVO.setAreaName(sysStoreArea.getName());
            clothPriceQueryByClothTypeVO.setName(clothItemVo.getName());
            clothPriceQueryByClothTypeVO.setClothId(clothItemVo.getId());
            clothPriceQueryByClothTypeVO.setStoreAreaId(clothPriceQueryByClothTypeDTO.getStoreAreaId());
            clothPriceQueryByClothTypeVO.setTypeId(Integer.parseInt(clothPriceQueryByClothTypeDTO.getClothType()));
            ClothPrice clothPrice = this.selectClothPriceByClothIdAndWashModeId(ClothPriceType.cloth_type.getCode(), clothItemVo.getId(), clothPriceQueryByClothTypeDTO.getStoreAreaId(), null, Integer.parseInt(clothPriceQueryByClothTypeDTO.getClothType()));
            clothPriceQueryByClothTypeVO.setPrice(clothPrice != null ? clothPrice.getPrice() : null);
            clothPriceQueryByClothTypeVOS.add(clothPriceQueryByClothTypeVO);
        }

        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(HttpStatus.SUCCESS);
        rspData.setMsg("查询成功");
        rspData.setRows(clothPriceQueryByClothTypeVOS);
        rspData.setTotal(new PageInfo(selectClothItemList).getTotal());
        return rspData;
    }

    @Override
    public TableDataInfo getClothPriceListByWashModeV2(ClothPriceQueryByWashModeDTO clothPriceQueryByWashModeDTO) {
        ClothItem clothItem = new ClothItem();
        clothItem.setName(clothPriceQueryByWashModeDTO.getName());
        List<ClothItemVo> selectClothItemList = clothItemService.selectClothItemList(clothItem);
        ClothWashMode clothWashMode = clothWashModeService.selectClothWashModeById(clothPriceQueryByWashModeDTO.getWashModeId());
        SysStoreArea sysStoreArea = sysStoreAreaService.selectSysStoreAreaById(clothPriceQueryByWashModeDTO.getStoreAreaId());
        List<ClothPriceQueryByWashModeVO> clothPriceQueryByClothTypeVOS = new ArrayList<>();
        for (ClothItemVo clothItemVo : selectClothItemList) {
            ClothPriceQueryByWashModeVO clothPriceQueryByWashModeVO = new ClothPriceQueryByWashModeVO();
            clothPriceQueryByWashModeVO.setWashModeName(clothWashMode.getName());
            clothPriceQueryByWashModeVO.setAreaName(sysStoreArea.getName());
            clothPriceQueryByWashModeVO.setName(clothItemVo.getName());
            clothPriceQueryByWashModeVO.setClothId(clothItemVo.getId());
            clothPriceQueryByWashModeVO.setWashModeId(clothPriceQueryByWashModeDTO.getWashModeId());
            clothPriceQueryByWashModeVO.setStoreAreaId(clothPriceQueryByWashModeDTO.getStoreAreaId());
            ClothPrice clothPrice = this.selectClothPriceByClothIdAndWashModeId(ClothPriceType.wash_node.getCode(), clothItemVo.getId(), clothPriceQueryByWashModeDTO.getStoreAreaId(), clothPriceQueryByWashModeDTO.getWashModeId(), null);
            clothPriceQueryByWashModeVO.setPrice(clothPrice != null ? clothPrice.getPrice() : null);
            clothPriceQueryByClothTypeVOS.add(clothPriceQueryByWashModeVO);
        }

        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(HttpStatus.SUCCESS);
        rspData.setMsg("查询成功");
        rspData.setRows(clothPriceQueryByClothTypeVOS);
        rspData.setTotal(new PageInfo(selectClothItemList).getTotal());
        return rspData;
    }

    @Override
    public int countClothPriceListByClothType(ClothPriceQueryByClothTypeDTO clothPriceQueryByClothTypeDTO) {
        return baseMapper.countClothPriceListByClothType(clothPriceQueryByClothTypeDTO);
    }

    @Override
    public List<ClothPriceQueryByWashModeVO> getClothPriceListByWashMode(ClothPriceQueryByWashModeDTO clothPriceQueryByWashModeDTO) {
        return baseMapper.getClothPriceListByWashMode(clothPriceQueryByWashModeDTO);
    }

    @Override
    public int countClothPriceListByWashMode(ClothPriceQueryByWashModeDTO clothPriceQueryByWashModeDTO) {
        return baseMapper.countClothPriceListByWashMode(clothPriceQueryByWashModeDTO);
    }

    @Override
    public List<ClothPriceExportVO> getClothPriceListByAreaIdExport(Long areaId) {
        List<ClothPriceExportVO> clothPriceExportVOS = new ArrayList<>();
        List<ClothPriceSetVO> priceSetVOList = clothItemService.selectClothItemPriceList(areaId, new ClothItem());
        SysStoreArea sysStoreArea = sysStoreAreaService.selectSysStoreAreaById(areaId);
        for (ClothPriceSetVO clothPriceSetVO : priceSetVOList) {
            ClothPriceExportVO clothPriceExportVO = new ClothPriceExportVO();
            clothPriceExportVO.setClothName(clothPriceSetVO.getName());
            clothPriceExportVO.setAreaId(areaId);
            clothPriceExportVO.setAreaName(sysStoreArea.getName());
            clothPriceExportVO.setClothId(clothPriceSetVO.getId());
            clothPriceExportVO.setTypeName(clothPriceSetVO.getTypeName());
            clothPriceExportVO.setThickeningPrice(clothPriceSetVO.getPlusPrice() != null ? clothPriceSetVO.getPlusPrice().toString() : "");
            clothPriceExportVO.setCashmerePrice(clothPriceSetVO.getSheepPrice() != null ? clothPriceSetVO.getSheepPrice().toString() : "");
            clothPriceExportVO.setSilkPrice(clothPriceSetVO.getSilkPrice() != null ? clothPriceSetVO.getSilkPrice().toString() : "");
            clothPriceExportVO.setWashPrice(clothPriceSetVO.getWashModePrice() != null ? clothPriceSetVO.getWashModePrice().toString() : "");
            clothPriceExportVO.setDarningPrice(clothPriceSetVO.getDarningModePrice() != null ? clothPriceSetVO.getDarningModePrice().toString() : "");
            clothPriceExportVO.setChildPrice(clothPriceSetVO.getChildPrice() != null ? clothPriceSetVO.getChildPrice().toString() : "");
            clothPriceExportVO.setDryCleaningPrice(clothPriceSetVO.getDryCleaningModePrice() != null ? clothPriceSetVO.getDryCleaningModePrice().toString() : "");
            clothPriceExportVO.setNurseModePrice(clothPriceSetVO.getNurseModePrice() != null ? clothPriceSetVO.getNurseModePrice().toString() : "");
            clothPriceExportVO.setFineWashModePrice(clothPriceSetVO.getFineWashModePrice() != null ? clothPriceSetVO.getFineWashModePrice().toString() : "");
            clothPriceExportVO.setPermPrice(clothPriceSetVO.getIronModePrice() != null ? clothPriceSetVO.getIronModePrice().toString() : "");
            clothPriceExportVO.setShehuModePrice(clothPriceSetVO.getShehuModePrice() != null ? clothPriceSetVO.getShehuModePrice().toString() : "");
            clothPriceExportVOS.add(clothPriceExportVO);
        }
        return clothPriceExportVOS;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String importData(List<ClothPriceImportVO> priceImportVOS, ClothPriceImportDTO clothPriceImportDTO) {
        if (CollUtil.isEmpty(priceImportVOS)) {
            throw new ServiceException("导入数据不能为空！");
        }
        List<Long> clothIds = priceImportVOS.stream()
                .map(ClothPriceImportVO::getId).collect(Collectors.toList());
        List<Long> duplicates = clothIds.stream()
                .distinct().filter(element -> clothIds.indexOf(element) != clothIds.lastIndexOf(element))
                .collect(Collectors.toList());
        if (duplicates != null && duplicates.size() > 0) {
            throw new ServiceException("导入数据存在重复的衣服编号:" + Arrays.toString(duplicates.toArray()));
        }
        // 将需要导入的区域价格删除
        this.remove(new QueryWrapper<ClothPrice>().lambda()
                .eq(ClothPrice::getStoreAreaId, clothPriceImportDTO.getStoreAreaId()));

        List<ClothPrice> clothPriceList = new ArrayList<>();
        for (ClothPriceImportVO importVO : priceImportVOS) {
            // 加厚
            if (importVO.getThickeningPrice() != null && importVO.getThickeningPrice().compareTo(BigDecimal.ZERO) > 0) {
                ClothPrice clothPrice1 = new ClothPrice();
                clothPrice1.setClothId(importVO.getId());
                clothPrice1.setStoreAreaId(clothPriceImportDTO.getStoreAreaId());
                clothPrice1.setPrice(importVO.getThickeningPrice());
                clothPrice1.setPriceType(ClothPriceType.cloth_type.getCode());
                clothPrice1.setClothType(ClothTypes.thickening.getCode());
                clothPrice1.buildCreateData();
                clothPriceList.add(clothPrice1);
            }

            if (importVO.getCashmerePrice() != null && importVO.getCashmerePrice().compareTo(BigDecimal.ZERO) > 0) {
                // 羊绒
                ClothPrice clothPrice2 = new ClothPrice();
                clothPrice2.setClothId(importVO.getId());
                clothPrice2.setStoreAreaId(clothPriceImportDTO.getStoreAreaId());
                clothPrice2.setPrice(importVO.getCashmerePrice());
                clothPrice2.setPriceType(ClothPriceType.cloth_type.getCode());
                clothPrice2.setClothType(ClothTypes.cashmere.getCode());
                clothPrice2.buildCreateData();

                clothPriceList.add(clothPrice2);
            }

            if (importVO.getSilkPrice() != null && importVO.getSilkPrice().compareTo(BigDecimal.ZERO) > 0) {
                // 真丝
                ClothPrice clothPrice3 = new ClothPrice();
                clothPrice3.setClothId(importVO.getId());
                clothPrice3.setStoreAreaId(clothPriceImportDTO.getStoreAreaId());
                clothPrice3.setPrice(importVO.getSilkPrice());
                clothPrice3.setPriceType(ClothPriceType.cloth_type.getCode());
                clothPrice3.setClothType(ClothTypes.silk.getCode());
                clothPrice3.buildCreateData();

                clothPriceList.add(clothPrice3);
            }

            if (importVO.getChildPrice() != null && importVO.getChildPrice().compareTo(BigDecimal.ZERO) > 0) {
                // 儿童
                ClothPrice clothPrice4 = new ClothPrice();
                clothPrice4.setClothId(importVO.getId());
                clothPrice4.setStoreAreaId(clothPriceImportDTO.getStoreAreaId());
                clothPrice4.setPrice(importVO.getChildPrice());
                clothPrice4.setPriceType(ClothPriceType.cloth_type.getCode());
                clothPrice4.setClothType(ClothTypes.child.getCode());
                clothPrice4.buildCreateData();

                clothPriceList.add(clothPrice4);
            }

            if (importVO.getDryCleaningPrice() != null && importVO.getDryCleaningPrice().compareTo(BigDecimal.ZERO) > 0) {
                // 干洗
                ClothPrice clothPrice5 = new ClothPrice();
                clothPrice5.setClothId(importVO.getId());
                clothPrice5.setStoreAreaId(clothPriceImportDTO.getStoreAreaId());
                clothPrice5.setPrice(importVO.getDryCleaningPrice());
                clothPrice5.setPriceType(ClothPriceType.wash_node.getCode());
                clothPrice5.setWashModeId(Long.valueOf(ClothWashModeType.dryCleaning.getCode()));
                clothPrice5.buildCreateData();

                clothPriceList.add(clothPrice5);
            }

            if (importVO.getWashPrice() != null && importVO.getWashPrice().compareTo(BigDecimal.ZERO) > 0) {
                // 水洗
                ClothPrice clothPrice6 = new ClothPrice();
                clothPrice6.setClothId(importVO.getId());
                clothPrice6.setStoreAreaId(clothPriceImportDTO.getStoreAreaId());
                clothPrice6.setPrice(importVO.getWashPrice());
                clothPrice6.setPriceType(ClothPriceType.wash_node.getCode());
                clothPrice6.setWashModeId(Long.valueOf(ClothWashModeType.wash.getCode()));
                clothPrice6.buildCreateData();

                clothPriceList.add(clothPrice6);
            }

            if (importVO.getDarningPrice() != null && importVO.getDarningPrice().compareTo(BigDecimal.ZERO) > 0) {
                // 织补
                ClothPrice clothPrice7 = new ClothPrice();
                clothPrice7.setClothId(importVO.getId());
                clothPrice7.setStoreAreaId(clothPriceImportDTO.getStoreAreaId());
                clothPrice7.setPrice(importVO.getDarningPrice());
                clothPrice7.setPriceType(ClothPriceType.wash_node.getCode());
                clothPrice7.setWashModeId(Long.valueOf(ClothWashModeType.darning.getCode()));
                clothPrice7.buildCreateData();

                clothPriceList.add(clothPrice7);
            }

            if (importVO.getPermPrice() != null && importVO.getPermPrice().compareTo(BigDecimal.ZERO) > 0) {

                // 单烫
                ClothPrice clothPrice8 = new ClothPrice();
                clothPrice8.setClothId(importVO.getId());
                clothPrice8.setStoreAreaId(clothPriceImportDTO.getStoreAreaId());
                clothPrice8.setPrice(importVO.getPermPrice());
                clothPrice8.setPriceType(ClothPriceType.wash_node.getCode());
                clothPrice8.setWashModeId(Long.valueOf(ClothWashModeType.perm.getCode()));
                clothPrice8.buildCreateData();

                clothPriceList.add(clothPrice8);
            }

            if (importVO.getFineWashModePrice() != null && importVO.getFineWashModePrice().compareTo(BigDecimal.ZERO) > 0) {

                // 保值精洗
                ClothPrice clothPrice9 = new ClothPrice();
                clothPrice9.setClothId(importVO.getId());
                clothPrice9.setStoreAreaId(clothPriceImportDTO.getStoreAreaId());
                clothPrice9.setPrice(importVO.getFineWashModePrice());
                clothPrice9.setPriceType(ClothPriceType.wash_node.getCode());
                clothPrice9.setWashModeId(Long.valueOf(ClothWashModeType.fineWashMode.getCode()));
                clothPrice9.buildCreateData();

                clothPriceList.add(clothPrice9);
            }

            if (importVO.getNurseModePrice() != null && importVO.getNurseModePrice().compareTo(BigDecimal.ZERO) > 0) {
                // 护理
                ClothPrice clothPrice10 = new ClothPrice();
                clothPrice10.setClothId(importVO.getId());
                clothPrice10.setStoreAreaId(clothPriceImportDTO.getStoreAreaId());
                clothPrice10.setPrice(importVO.getNurseModePrice());
                clothPrice10.setPriceType(ClothPriceType.wash_node.getCode());
                clothPrice10.setWashModeId(Long.valueOf(ClothWashModeType.nurseMode.getCode()));
                clothPrice10.buildCreateData();

                clothPriceList.add(clothPrice10);
            }

            if (importVO.getShehuModePrice() != null && importVO.getShehuModePrice().compareTo(BigDecimal.ZERO) > 0) {
                // 奢护洗
                ClothPrice clothPrice11 = new ClothPrice();
                clothPrice11.setClothId(importVO.getId());
                clothPrice11.setStoreAreaId(clothPriceImportDTO.getStoreAreaId());
                clothPrice11.setPrice(importVO.getShehuModePrice());
                clothPrice11.setPriceType(ClothPriceType.wash_node.getCode());
                clothPrice11.setWashModeId(Long.valueOf(ClothWashModeType.shehuMode.getCode()));
                clothPrice11.buildCreateData();

                clothPriceList.add(clothPrice11);
            }

        }
        this.saveBatch(clothPriceList);
        return "导入成功";
    }


    /**
     * 查询衣服价格
     *
     * @param id 衣服价格主键
     * @return 衣服价格
     */
    @Override
    public ClothPrice selectClothPriceById(Long id) {
        return baseMapper.selectClothPriceById(id);
    }

    @Override
    public ClothPrice selectClothPriceByClothIdAndWashModeId(Integer priceType, Long clothId, Long storeAreaId, Long washModeId, Integer clothType) {
        List<ClothPrice> clothPriceList = baseMapper.selectClothPriceByClothIdAndWashModeId(priceType, clothId, storeAreaId, washModeId, clothType);
        if (clothPriceList.size() > 1) {
            throw new ServiceException("存在多条价格，数据存在异常");
        }
        if (clothPriceList.size() == 0) {
            return null;
        }
        return clothPriceList.get(0);
    }

    /**
     * 查询衣服价格列表
     *
     * @param clothPrice 衣服价格
     * @return 衣服价格
     */
    @Override
    public List<ClothPrice> selectClothPriceList(ClothPrice clothPrice) {
        return baseMapper.selectClothPriceList(clothPrice);
    }

    /**
     * 查询衣服价格列表
     *
     * @param clothItem 衣服价格
     * @return 衣服价格
     */
    @Override
    public List<ClothPriceSetVO> selectClothPriceSetList(ClothItem clothItem, Long storeAreaId) {
        return baseMapper.selectClothPriceSetList(clothItem, storeAreaId);
    }

    /**
     * 新增衣服价格
     *
     * @param clothPrice 衣服价格
     * @return 结果
     */
    @Override
    public int insertClothPrice(ClothPrice clothPrice) {
        clothPrice.setCreateTime(DateUtils.getNowDate());
        return baseMapper.insertClothPrice(clothPrice);
    }

    /**
     * 修改衣服价格
     *
     * @param clothPrice 衣服价格
     * @return 结果
     */
    @Override
    public int updateClothPrice(ClothPrice clothPrice) {
        clothPrice.setUpdateTime(DateUtils.getNowDate());
        return baseMapper.updateClothPrice(clothPrice);
    }

    /**
     * 批量删除衣服价格
     *
     * @param ids 需要删除的衣服价格主键
     * @return 结果
     */
    @Override
    public int deleteClothPriceByIds(Long[] ids) {
        return baseMapper.deleteClothPriceByIds(ids);
    }

    /**
     * 删除衣服价格信息
     *
     * @param id 衣服价格主键
     * @return 结果
     */
    @Override
    public int deleteClothPriceById(Long id) {
        return baseMapper.deleteClothPriceById(id);
    }


}
