package com.zjrs.zwnw.sxgl.blo.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang.StringUtils;
import org.mohrss.leaf.core.framework.domain.blo.impl.BLOImpl;
import org.mohrss.leaf.core.framework.persistens.PageResult;
import org.mohrss.leaf.core.framework.persistens.PageUtil;
import org.mohrss.leaf.core.framework.util.CurrentUser;
import org.mohrss.leaf.core.framework.util.SecurityUtils;
import com.zjrs.zwnw.sxgl.blo.SxclwhBLO;
import com.zjrs.zwnw.sxgl.dto.SxclwhDTO;
import com.zjrs.zwnw.sxgl.dto.SxclwhDeleteDTO;
import com.zjrs.zwnw.sxgl.dto.SxclwhQueryDTO;
import com.zjrs.zwnw.sxgl.entity.SdszzcxxMapper;
import com.zjrs.zwnw.sxgl.entity.SxclwhMapper;
import com.zjrs.zwnw.sxgl.entity.SxpzxxMapper;
import com.zjrs.zwnw.sxgl.po.SdszzcxxPO;
import com.zjrs.zwnw.sxgl.po.SdzlsxPO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import jakarta.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class SxclwhBLOimpl extends BLOImpl implements SxclwhBLO {
    @Resource
    SxclwhMapper sxclwhMapper;
    @Resource
    SdszzcxxMapper sdszzcxxMapper;
    @Resource
    SxpzxxMapper sxpzxxMapper;

    @Override
    public PageResult querySxclList(SxclwhQueryDTO dto, int page, int rows) {
        QueryWrapper<SdzlsxPO> queryWrapper = new QueryWrapper<>();
        //按操作时间降序
        queryWrapper.eq("ts_yxbz", "1").eq("ssbmmc",dto.getSsbmmc());//有效
        //材料名称-模糊
        if (StringUtils.isNotBlank(dto.getZlmc())) {
            queryWrapper.like("zlmc", dto.getZlmc());
        }

        Page<SdzlsxPO> page1 = new Page<>(page, rows);
        Page<SdzlsxPO> pageResult = (Page) this.sxclwhMapper.selectPage(page1, queryWrapper);
        PageResult result = PageUtil.PageResult(pageResult);
        return result;
    }

    @Override
    public void addSxcl(SxclwhDTO dto) {
        //从上下文中获取当前登陆用户
        CurrentUser cUser = SecurityUtils.getCurrentUser();
        //操作时间
        Date t = new Date();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        SdzlsxPO sdzlsxPo = new SdzlsxPO();
        BeanUtils.copyProperties(dto, sdzlsxPo);
        sdzlsxPo.setSsbmmc(cUser.getRegioncode());
        this.sxclwhMapper.insert(sdzlsxPo);
    }

    @Override
    public Map<String, String> deleteSxclByIds(SxclwhDeleteDTO dto) {
        Map<String, String> resultMap = new HashMap<>();
        StringBuilder errMsg = new StringBuilder();
        //从上下文中获取当前登陆用户
        CurrentUser cUser = SecurityUtils.getCurrentUser();
        //操作时间
        Date t = new Date();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String ids = dto.getIds();
        String[] idArr = ids.split(",");
        if (idArr != null && idArr.length != 0) {
            if (errMsg.length() > 0) {
                errMsg.deleteCharAt(errMsg.length() - 1);
                errMsg.append("材料已关联事项并发布，正在使用中的材料不允许删除！");
                resultMap.put("code", "1");
                resultMap.put("msg", errMsg.toString());
                return resultMap;
            }
            List<String> list = Arrays.asList(idArr);

            UpdateWrapper<SdszzcxxPO> sdszzcxxPOUpdateWrapper = new UpdateWrapper<>();
            sdszzcxxPOUpdateWrapper.in("zllbid", list);
            //删除关联关系
            sdszzcxxMapper.delete(sdszzcxxPOUpdateWrapper);
            //逻辑删除材料
            UpdateWrapper<SdzlsxPO> sdzlsxPOUpdateWrapper = new UpdateWrapper<>();
            sdzlsxPOUpdateWrapper.set("ts_yxbz", "0")
                    .in("zllbid", list);
//            sxclwhMapper.deleteBatchIds(list);
            sxclwhMapper.update(null, sdzlsxPOUpdateWrapper);

        }
        return resultMap;
    }

    @Override
    public SdzlsxPO getSxclById(SxclwhQueryDTO dto) {
        QueryWrapper<SdzlsxPO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("zllbid", dto.getZllbid());
        return this.sxclwhMapper.selectOne(queryWrapper);
    }

    @Override
    public void updateSxcl(SxclwhDTO dto) {
        //从上下文中获取当前登陆用户
        CurrentUser cUser = SecurityUtils.getCurrentUser();
        //获取用户ID
        cUser.getUserid();
        //操作时间
        Date t = new Date();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        SdzlsxPO sdzlsxPo = new SdzlsxPO();
        BeanUtils.copyProperties(dto, sdzlsxPo);
        sdzlsxPo.setTsCzsj(df.format(t));
        //判断是否支持电子证照是否改变
        SdzlsxPO checkPO = this.sxclwhMapper.selectById(dto.getZllbid());
        if (StringUtils.isNotBlank(dto.getSfzcdzzz()) && !dto.getSfzcdzzz().equals(checkPO.getSfzcdzzz())) {
            //不同，更新所有已经关联的材料注册信息
            UpdateWrapper<SdszzcxxPO> updateWrapper = new UpdateWrapper<>();
            this.sdszzcxxMapper.update(null, updateWrapper);
        }
        this.sxclwhMapper.updateById(sdzlsxPo);
    }
}
