package com.ruoyi.system.service.impl;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ruoyi.common.constant.HttpStatus;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.DoumuProductFj;
import com.ruoyi.system.domain.vo.DoumuProductInfoVo;
import com.ruoyi.system.mapper.DoumuProductFjMapper;
import com.ruoyi.system.mapper.SysDictDataMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.DoumuProductInfoMapper;
import com.ruoyi.system.domain.DoumuProductInfo;
import com.ruoyi.system.service.IDoumuProductInfoService;

/**
 * 痘姆古陶信息Service业务层处理
 *
 * @author boman
 * @date 2024-01-16
 */
@Service
public class DoumuProductInfoServiceImpl implements IDoumuProductInfoService
{
    @Autowired
    private DoumuProductInfoMapper doumuProductInfoMapper;

    @Autowired
    private DoumuProductFjMapper doumuProductFjMapper;

    @Autowired
    private SysDictDataMapper dictDataMapper;

    /**
     * 查询痘姆古陶信息
     *
     * @param productId 痘姆古陶信息主键
     * @return 痘姆古陶信息
     */
    @Override
    public DoumuProductInfo selectDoumuProductInfoByProductId(Long productId)
    {
        DoumuProductInfo doumuProductInfo = doumuProductInfoMapper.selectDoumuProductInfoByProductId(productId);
        //查询附件
        DoumuProductFj doumuProductFj = new DoumuProductFj();
        doumuProductFj.setProductId(productId);
        List<DoumuProductFj> fjList = doumuProductFjMapper.selectDoumuProductFjList(doumuProductFj);
        doumuProductInfo.setFjList(fjList);
        return doumuProductInfo;
    }

    /**
     * 查询痘姆古陶信息列表
     *
     * @param doumuProductInfo 痘姆古陶信息
     * @return 痘姆古陶信息
     */
    @Override
    public List<DoumuProductInfo> selectDoumuProductInfoList(DoumuProductInfo doumuProductInfo)
    {
        List<DoumuProductInfo> doumuProductInfoList = doumuProductInfoMapper.selectDoumuProductInfoList(doumuProductInfo);
        for (DoumuProductInfo productInfo : doumuProductInfoList) {
            //查询附件
            DoumuProductFj doumuProductFj = new DoumuProductFj();
            doumuProductFj.setProductId(productInfo.getProductId());
            List<DoumuProductFj> fjList = doumuProductFjMapper.selectDoumuProductFjList(doumuProductFj);
            productInfo.setFjList(fjList);
        }
        return doumuProductInfoList;
    }

    /**
     * 新增痘姆古陶信息
     *
     * @param doumuProductInfo 痘姆古陶信息
     * @return 结果
     */
    @Override
    public int insertDoumuProductInfo(DoumuProductInfo doumuProductInfo)
    {
        doumuProductInfo.setOrderNumber("DMGT"+System.currentTimeMillis());
        doumuProductInfo.setCreateTime(DateUtils.getNowDate());
        int i = doumuProductInfoMapper.insertDoumuProductInfo(doumuProductInfo);

        //保存附件
        List<DoumuProductFj> fjList = doumuProductInfo.getFjList();
        if(fjList!=null && fjList.size()>0){
            for (DoumuProductFj doumuProductFj : fjList) {
                doumuProductFj.setProductId(doumuProductInfo.getProductId());
                doumuProductFjMapper.insertDoumuProductFj(doumuProductFj);
            }
        }
        return i;
    }

    /**
     * 修改痘姆古陶信息
     *
     * @param doumuProductInfo 痘姆古陶信息
     * @return 结果
     */
    @Override
    public int updateDoumuProductInfo(DoumuProductInfo doumuProductInfo)
    {
        doumuProductInfo.setUpdateTime(DateUtils.getNowDate());

        //保存附件
        List<DoumuProductFj> fjList = doumuProductInfo.getFjList();
        if(fjList!=null && fjList.size()>0){
            for (DoumuProductFj doumuProductFj : fjList) {
                //先删除附件
                doumuProductFjMapper.deleteDoumuProductFjByFiId(doumuProductFj.getFiId());
                //在保存附件
                doumuProductFj.setProductId(doumuProductInfo.getProductId());
                doumuProductFjMapper.insertDoumuProductFj(doumuProductFj);
            }
        }

        return doumuProductInfoMapper.updateDoumuProductInfo(doumuProductInfo);
    }

    /**
     * 批量删除痘姆古陶信息
     *
     * @param productIds 需要删除的痘姆古陶信息主键
     * @return 结果
     */
    @Override
    public int deleteDoumuProductInfoByProductIds(Long[] productIds)
    {
        return doumuProductInfoMapper.deleteDoumuProductInfoByProductIds(productIds);
    }

    /**
     * 删除痘姆古陶信息信息
     *
     * @param productId 痘姆古陶信息主键
     * @return 结果
     */
    @Override
    public int deleteDoumuProductInfoByProductId(Long productId)
    {
        return doumuProductInfoMapper.deleteDoumuProductInfoByProductId(productId);
    }

    /**
     * 批量修改流程
     *
     * @param doumuProductInfoVo 痘姆古陶信息VO
     * @return 结果
     */
    @Override
    public int editFlow(DoumuProductInfoVo doumuProductInfoVo) {
        if(doumuProductInfoVo.getProductIdList() == null || doumuProductInfoVo.getProductIdList().size()<=0){
            throw new ServiceException("请选择要修改的订单", HttpStatus.WARN);
        }
        List<Long> productIdList = doumuProductInfoVo.getProductIdList();
        String flowType = doumuProductInfoVo.getFlowType();
        return doumuProductInfoMapper.updateDoumuProductInfoFlowList(productIdList,flowType);
    }

    @Override
    public AjaxResult link(String dateType) {
        Map<String,Object> map = new HashMap<>();
        String startTime = DateUtils.getTime(dateType);
        String endTime = DateUtils.getTime("1");
        //查询时间内地数据
        SysDictData dictData = new SysDictData();
        dictData.setDictType("liuch");
        List<SysDictData> sysDictDataList = dictDataMapper.selectDictDataList(dictData);
        //计算总数
        DoumuProductInfo doumuProductInfo = new DoumuProductInfo();
        doumuProductInfo.setBeginTime(startTime);
        doumuProductInfo.setEndTime(endTime);
        List<DoumuProductInfo> doumuProductInfoList = doumuProductInfoMapper.selectDoumuProductInfoList(doumuProductInfo);
        if(doumuProductInfoList==null || doumuProductInfoList.size()<=0){
            map.put("all",0);
            for (SysDictData sysDictData : sysDictDataList) {
                map.put(sysDictData.getDictValue(),0);
            }
            return AjaxResult.success(map);
        }
        map.put("all",doumuProductInfoList.size());
        for (SysDictData sysDictData : sysDictDataList) {
            int num = 0;
            for (DoumuProductInfo productInfo : doumuProductInfoList) {
                if(sysDictData.getDictValue().equals(productInfo.getFlowType())){
                    num++;
                }
            }
            map.put(sysDictData.getDictValue(),num);
        }
        return AjaxResult.success(map);
    }

    @Override
    public AjaxResult monthLink() {
        Map<String,Object> map = new HashMap<>();
        String startTime = DateUtils.getTime("3");
        String endTime = DateUtils.getTime("1");
        //查询时间内地数据
        SysDictData dictData = new SysDictData();
        dictData.setDictType("liuch");
        List<SysDictData> sysDictDataList = dictDataMapper.selectDictDataList(dictData);
        //计算总数
        DoumuProductInfo doumuProductInfo = new DoumuProductInfo();
        doumuProductInfo.setBeginTime(startTime);
        doumuProductInfo.setEndTime(endTime);
        List<DoumuProductInfo> doumuProductInfoList = doumuProductInfoMapper.selectDoumuProductInfoList(doumuProductInfo);
        if(doumuProductInfoList==null || doumuProductInfoList.size()<=0){
            for (SysDictData sysDictData : sysDictDataList) {
                map.put(sysDictData.getDictValue(),"0%");
            }
            return AjaxResult.success(map);
        }

        int count = doumuProductInfoList.size();
        BigDecimal num1 = new BigDecimal(count);
        for (SysDictData sysDictData : sysDictDataList) {
            int num = 0;
            for (DoumuProductInfo productInfo : doumuProductInfoList) {
                if(sysDictData.getDictValue().equals(productInfo.getFlowType())){
                    num++;
                }
            }
            BigDecimal num2 = new BigDecimal(num);
            double result = Double.parseDouble(((num1.divide(num2,2,BigDecimal.ROUND_HALF_UP))).toString());
            // 创建DecimalFormat对象并设置格式化模式为"0%"
            DecimalFormat decimalFormat = new DecimalFormat("0%");
            // 使用DecimalFormat对象进行格式化操作
            String percentage = decimalFormat.format((result));
            map.put(sysDictData.getDictValue(),percentage);
        }
        return AjaxResult.success(map);
    }
}
