package com.ruoyi.project.business.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.MessageUtils;
import com.ruoyi.project.business.domain.TcProjectEquipment;
import com.ruoyi.project.business.domain.vo.project.ProjectEquipmentReq;
import com.ruoyi.project.business.domain.vo.project.ProjectSaveOrUpdateEquipmentReq;
import com.ruoyi.project.business.mapper.TcProjectEquipmentMapper;
import com.ruoyi.project.business.service.ITcProjectEquipmentService;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Collections;
import java.util.List;
import java.util.Map;


/**
 * 【请填写功能名称】Service业务层处理
 *
 * @author ruoyi
 * @date 2024-06-28
 */
@Service("tcProjectEquipmentService")
public class TcProjectEquipmentServiceImpl extends ServiceImpl<TcProjectEquipmentMapper, TcProjectEquipment> implements ITcProjectEquipmentService {
    @Resource
    private TcProjectEquipmentMapper tcProjectEquipmentMapper;

    /**
     * 查询【请填写功能名称】
     *
     * @param id 【请填写功能名称】主键
     * @return 【请填写功能名称】
     */
    @Override
    public TcProjectEquipment selectTcProjectEquipmentById(Long id) {
        return tcProjectEquipmentMapper.selectTcProjectEquipmentById(id);
    }

    /**
     * 查询【请填写功能名称】列表
     *
     * @param req 【请填写功能名称】
     * @return 【请填写功能名称】
     */
    @Override
    public List<TcProjectEquipment> selectTcProjectEquipmentList(ProjectEquipmentReq req) {
        // return tcProjectEquipmentMapper.selectTcProjectEquipmentList(req);
        LambdaQueryWrapper<TcProjectEquipment> wrapper = new LambdaQueryWrapper<TcProjectEquipment>()
                .eq(req.getProjectId() != null, TcProjectEquipment::getProjectId, req.getProjectId())
                .eq(req.getEquipmentQuantity() != null, TcProjectEquipment::getEquipmentQuantity, req.getEquipmentQuantity())
                .eq(StringUtils.isNotBlank(req.getEquipmentType()), TcProjectEquipment::getEquipmentType, req.getEquipmentType())
                .eq(req.getAmount() != null, TcProjectEquipment::getAmount, req.getAmount())
                .notIn(CollectionUtils.isNotEmpty(req.getRemoveIds()), TcProjectEquipment::getId, req.getRemoveIds());
        return list(wrapper);
    }

    /**
     * 新增【请填写功能名称】
     *
     * @param tcProjectEquipment 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int insertTcProjectEquipment(TcProjectEquipment tcProjectEquipment) {
        tcProjectEquipment.setCreateTime(DateUtils.getNowDate());
        return tcProjectEquipmentMapper.insertTcProjectEquipment(tcProjectEquipment);
    }

    /**
     * 修改【请填写功能名称】
     *
     * @param tcProjectEquipment 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int updateTcProjectEquipment(TcProjectEquipment tcProjectEquipment) {
        tcProjectEquipment.setUpdateTime(DateUtils.getNowDate());
        return tcProjectEquipmentMapper.updateTcProjectEquipment(tcProjectEquipment);
    }

    /**
     * 批量删除【请填写功能名称】
     *
     * @param ids 需要删除的【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteTcProjectEquipmentByIds(Long[] ids) {
        return tcProjectEquipmentMapper.deleteTcProjectEquipmentByIds(ids);
    }

    /**
     * 删除【请填写功能名称】信息
     *
     * @param id 【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteTcProjectEquipmentById(Long id) {
        return tcProjectEquipmentMapper.deleteTcProjectEquipmentById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveOrUpdateEquipment(ProjectSaveOrUpdateEquipmentReq req) {
        List<TcProjectEquipment> list = req.getSaveOrUpdateList();
        for (TcProjectEquipment equipment : list) {
            equipment.setTotalPrice(equipment.getAmount().multiply(BigDecimal.valueOf(equipment.getEquipmentQuantity())));
        }
        saveOrUpdateBatch(list);
        removeBatchByIds(req.getRemoveIds());
        return true;
    }

    @Override
    public Map<String, Object> selectTotal(ProjectEquipmentReq req) {
        QueryWrapper<TcProjectEquipment> wrapper = new QueryWrapper<>();
        // 总计金额
        wrapper.select("SUM(equipment_quantity) AS totalCount", "SUM(total_price) AS totalAmount")
                .eq(req.getProjectId() != null, "project_id", req.getProjectId())
                .eq(req.getEquipmentQuantity() != null, "equipment_quantity", req.getEquipmentQuantity())
                .eq(StringUtils.isNotBlank(req.getEquipmentType()), "equipment_type", req.getEquipmentType())
                .eq(req.getAmount() != null, "amount", req.getAmount())
                .eq("del_flag", "0")
                .notIn(CollectionUtils.isNotEmpty(req.getRemoveIds()), "id", req.getRemoveIds());
        
        Map<String, Object> map = getMap(wrapper);
        if (map == null) {
            return Collections.emptyMap();
        }
        return map;
    }

    /**
     * 查询【请填写功能名称】列表
     *
     * @param req 【请填写功能名称】
     * @return 【请填写功能名称】
     */
    @Override
    public List<TcProjectEquipment> selectTcProjectEquipmentListProject(ProjectEquipmentReq req) {
        List<TcProjectEquipment> list = tcProjectEquipmentMapper.selectTcProjectEquipmentListProject(req);
        for (TcProjectEquipment item : list){
            String org = "project".equals(item.getOrigin())? MessageUtils.message("project.origin.equipment")
                    :MessageUtils.message("project.origin.quotation");
            item.setOrigin(org);
        }

        return list;
    }

    @Override
    public Map<String, Object> selectTotalProject(ProjectEquipmentReq req) {
        return tcProjectEquipmentMapper.selectTotalProject(req);
    }
}
