package vip.xiaonuo.write.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import vip.xiaonuo.common.pojo.CommonResult;
import vip.xiaonuo.hr.modular.baseemployeeinfo.entity.HrBaseEmployeeInfo;
import vip.xiaonuo.write.entity.WriteCheck;
import vip.xiaonuo.write.entity.WriteList;
import vip.xiaonuo.write.entity.WriteUnqualified;
import vip.xiaonuo.write.entity.vo.WriteUnqualifiedVo;
import vip.xiaonuo.write.mapper.WriteListMapper;
import vip.xiaonuo.write.mapper.WriteUnqualifiedMapper;
import vip.xiaonuo.write.param.WriteUnqualifiedEditParam;
import vip.xiaonuo.write.param.WriteUnqualifiedPageParam;
import vip.xiaonuo.write.param.WriteUnqualifiedParam;
import vip.xiaonuo.write.service.IWriteUnqualifiedService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 不合格品台账 服务实现类
 * </p>
 *
 * @author author
 * @since 2025-01-02
 */
@Service
public class WriteUnqualifiedServiceImpl extends ServiceImpl<WriteUnqualifiedMapper, WriteUnqualified> implements IWriteUnqualifiedService {

    @Autowired
    private WriteUnqualifiedMapper unqualifiedMapper;

    @Autowired
    private WriteListMapper listMapper;

    @Override
    @Transactional
    public void add(WriteUnqualifiedParam param) {
        List<WriteUnqualified> unqualifieds = param.getWriteUnqualifieds();
        String year = param.getYear();
        Date editTime = param.getEditTime();
        Date examineTime = param.getExamineTime();
        String orgId = param.getOrgId();
        WriteList writeList = new WriteList();
        writeList.setType(0);
        writeList.setEditTime(editTime);
        writeList.setExamineTime(examineTime);
        writeList.setYear(year);
        writeList.setOrgId(orgId);
        writeList.setDate(new Date());
        String name = "";
        for (int i = 0; i < unqualifieds.size(); i++) {
            if (i < unqualifieds.size() - 1) {
                name = unqualifieds.get(i).getProduceName() + "," + name;
            } else {
                name = name + unqualifieds.get(i).getProduceName();
            }
        }
        writeList.setName(name);
        listMapper.insert(writeList);
        WriteList writeList1 = listMapper.selectOne(new QueryWrapper<WriteList>().orderByDesc("id").last(" limit 1"));
        for (WriteUnqualified unqualified : unqualifieds) {
            unqualified.setListId(writeList1.getId());
            unqualified.setYear(year);
            unqualified.setEditTime(editTime);
            unqualified.setExamineTime(examineTime);
            unqualified.setOrgId(orgId);
            unqualifiedMapper.insert(unqualified);
        }
    }

    @Override
    @Transactional
    public void update(WriteUnqualifiedParam editParam) {
        String year = editParam.getYear();
        Date editTime = editParam.getEditTime();
        Date examineTime = editParam.getExamineTime();
        String orgId = editParam.getOrgId();
        Integer listId = editParam.getListId();
        List<WriteUnqualified> unqualifieds = editParam.getWriteUnqualifieds();
        for (WriteUnqualified unqualified : unqualifieds) {
            WriteUnqualified writeUnqualified = unqualifiedMapper.selectById(unqualified.getId());
            if (ObjectUtil.isEmpty(writeUnqualified)) {
                unqualified.setYear(year);
                unqualified.setEditTime(editTime);
                unqualified.setExamineTime(examineTime);
                unqualified.setOrgId(orgId);
                unqualified.setListId(listId);
                unqualifiedMapper.insert(unqualified);
            } else {
                unqualifiedMapper.updateById(unqualified);
            }
        }
        WriteList writeList = listMapper.selectById(listId);
        String name = "";
        for (int i = 0; i < unqualifieds.size(); i++) {
            if (i < unqualifieds.size() - 1) {
                name = unqualifieds.get(i).getProduceName() + "," + name;
            } else {
                name = name + unqualifieds.get(i).getProduceName();
            }
        }
        writeList.setName(name);
        listMapper.updateById(writeList);
    }

    @Override
    public CommonResult page(WriteUnqualifiedPageParam pageParam) {
        Integer listId = pageParam.getListId();
        QueryWrapper<WriteUnqualified> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("list_id", listId);
        List<WriteUnqualified> unqualifieds = unqualifiedMapper.selectList(queryWrapper);
        QueryWrapper<WriteList> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("id", listId);
        WriteList writeList = listMapper.selectOne(queryWrapper1);
        WriteUnqualifiedVo vo = new WriteUnqualifiedVo();
        if (ObjectUtil.isNotEmpty(writeList)) {
            vo.setWriteUnqualifieds(unqualifieds);
            vo.setEditTime(writeList.getEditTime());
            vo.setExamineTime(writeList.getExamineTime());
            vo.setOrgId(writeList.getOrgId());
            vo.setYear(writeList.getYear());
            vo.setListId(writeList.getId());
        }
        return CommonResult.data(vo);
    }

    @Override
    public void delete(Map<String, String> paramsMap) {
        String id = paramsMap.get("id");
        WriteList writeList = listMapper.selectById(id);
        List<WriteUnqualified> writeChecks = unqualifiedMapper.selectList(new QueryWrapper<WriteUnqualified>().eq("list_id", id));
        for (WriteUnqualified writeCheck : writeChecks) {
            unqualifiedMapper.deleteById(writeCheck);
        }
        listMapper.deleteById(writeList);
    }
}
