package vip.xiaonuo.write.service.impl;

import cn.hutool.core.bean.BeanUtil;
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.WriteList;
import vip.xiaonuo.write.entity.WriteUnqualified;
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();
        WriteList writeList = new WriteList();
        writeList.setType(0);
        writeList.setOrgId(param.getWriteUnqualifieds().get(0).getOrgId());
        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());
            unqualifiedMapper.insert(unqualified);
        }
    }

    @Override
    @Transactional
    public void update(WriteUnqualifiedEditParam editParam) {
        WriteUnqualified unqualified = unqualifiedMapper.selectById(editParam.getId());
        BeanUtil.copyProperties(editParam, unqualified);
        unqualifiedMapper.updateById(unqualified);
    }

    @Override
    public CommonResult page(WriteUnqualifiedPageParam pageParam) {
        Integer listId = pageParam.getListId();
        Page<WriteUnqualified> page = new Page<>(pageParam.getPageNum(), pageParam.getPageSize());
        QueryWrapper<WriteUnqualified> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("list_id", listId);
        return CommonResult.data(unqualifiedMapper.selectPage(page, queryWrapper));
    }

    @Override
    public void delete(Map<String, String> paramsMap) {
        String id = paramsMap.get("id");
        unqualifiedMapper.deleteById(id);

    }
}
