package vip.xiaonuo.write.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
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.write.entity.WriteCheck;
import vip.xiaonuo.write.entity.WriteList;
import vip.xiaonuo.write.entity.WriteUnqualified;
import vip.xiaonuo.write.mapper.WriteCheckMapper;
import vip.xiaonuo.write.mapper.WriteListMapper;
import vip.xiaonuo.write.param.WriteCheckAddParam;
import vip.xiaonuo.write.param.WriteCheckEditParam;
import vip.xiaonuo.write.param.WriteUnqualifiedPageParam;
import vip.xiaonuo.write.service.IWriteCheckService;
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-08
 */
@Service
public class WriteCheckServiceImpl extends ServiceImpl<WriteCheckMapper, WriteCheck> implements IWriteCheckService {

    @Autowired
    private WriteListMapper listMapper;

    @Autowired
    private WriteCheckMapper checkMapper;

    @Override
    @Transactional
    public void add(WriteCheckAddParam addParam) {

        List<WriteCheck> writeChecks = addParam.getWriteChecks();
        WriteList writeList = new WriteList();
        writeList.setDate(new Date());
        writeList.setType(1);
        writeList.setOrgId(writeChecks.get(0).getOrgId());
        writeList.setDate(new Date());
        String name = "";
        for (int i = 0; i < writeChecks.size(); i++) {
            if (i < writeChecks.size() - 1) {
                name = writeChecks.get(i).getRecordName() + "," + name;
            } else {
                name = name + writeChecks.get(i).getRecordName();
            }
        }
        writeList.setName(name);
        listMapper.insert(writeList);

        WriteList writeList1 = listMapper.selectOne(new QueryWrapper<WriteList>().orderByDesc("id").last(" limit 1"));
        for (WriteCheck writeCheck : writeChecks) {
            writeCheck.setListId(writeList1.getId());
            checkMapper.insert(writeCheck);
        }
    }

    @Override
    @Transactional
    public void update(WriteCheckEditParam editParam) {
        WriteCheck writeCheck = checkMapper.selectById(editParam.getId());
        BeanUtil.copyProperties(editParam, writeCheck);
        checkMapper.updateById(writeCheck);
    }

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

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