package com.changhu.module.miniProgram.service.impl;

import cn.binarywang.wx.miniapp.api.WxMaQrcodeService;
import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.toolkit.Db;
import com.changhu.common.exception.MessageException;
import com.changhu.common.utils.UserUtil;
import com.changhu.module.management.mapper.ServiceProjectMapper;
import com.changhu.module.miniProgram.pojo.entity.SecurityUser;
import com.changhu.module.miniProgram.pojo.params.SaveOrUpdateSecurityUserParams;
import com.changhu.module.miniProgram.pojo.vo.IndexServiceProjectListVo;
import com.changhu.module.miniProgram.service.ProjectManageIndexService;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.error.WxErrorException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.List;

/**
 * @author 20252
 * @createTime 2024/9/10 下午3:50
 * @desc ProjectManageIndexServiceImpl...
 */
@Slf4j
@Service
public class ProjectManageIndexServiceImpl implements ProjectManageIndexService {

    @Autowired
    private ServiceProjectMapper serviceProjectMapper;

    @Autowired
    private WxMaService wxMaService;

    @Override
    public List<IndexServiceProjectListVo> getMyServiceProjectList() {
        return serviceProjectMapper.getServiceProjectList(null, UserUtil.getUserId());
    }

    @Override
    public void deleteSecurityUserByServiceProjectId(Long securityUserId) {
        boolean b = Db.removeById(securityUserId, SecurityUser.class);
        if (!b) {
            throw new MessageException();
        }
    }

    @Override
    public void saveOrUpdateSecurityUser(SaveOrUpdateSecurityUserParams params) {
        SecurityUser securityUser = BeanUtil.copyProperties(params, SecurityUser.class);
        //新增的情况
        Long snowFlakeId = securityUser.getSnowFlakeId();
        if (snowFlakeId == null) {
            //判断是否已经存在
            boolean exists = Db.lambdaQuery(SecurityUser.class)
                    .eq(SecurityUser::getServiceProjectId, securityUser.getServiceProjectId())
                    .eq(SecurityUser::getIdCard, securityUser.getIdCard())
                    .exists();
            if (exists) {
                throw new MessageException("服务项目下已经存在该人员");
            }
        } else {
            //如果修改了身份证 需要查重
            SecurityUser byId = Db.getById(snowFlakeId, SecurityUser.class);
            if (!securityUser.getIdCard().equals(byId.getIdCard())) {
                boolean exists = Db.lambdaQuery(SecurityUser.class)
                        .eq(SecurityUser::getServiceProjectId, securityUser.getServiceProjectId())
                        .eq(SecurityUser::getIdCard, securityUser.getIdCard())
                        .exists();
                if (exists) {
                    throw new MessageException("服务项目下已经存在该人员");
                }
            }
        }
        boolean b = Db.saveOrUpdate(securityUser);
        if (!b) {
            throw new MessageException();
        }
    }

    @Override
    public ResponseEntity<Resource> shareForm_QR_Code(String path, Integer width) {
        WxMaQrcodeService qrcodeService = wxMaService.getQrcodeService();
        try {
            File qrcodeFile = qrcodeService.createQrcode(path, width);
            return ResponseEntity.ok()
                    .header(HttpHeaders.CONTENT_DISPOSITION, "attachment;filename=" + qrcodeFile.getName())
                    .contentType(MediaType.APPLICATION_OCTET_STREAM)
                    .body(new FileSystemResource(qrcodeFile));
        } catch (WxErrorException e) {
            throw new MessageException("生成表单二维码失败:{}", e.getMessage());
        }
    }
}
