package com.css.txw.mhzc.service.impl;

import static com.css.ggzc.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.css.txw.mhzc.constants.ErrorCodeConstants.OAUTH2_LOGIN_SJHM_NOT_EXISTS;
import static com.css.txw.mhzc.constants.ErrorCodeConstants.YHXX_IS_EXISTS;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.css.ggzc.framework.cache.utils.CacheUtils;
import com.css.ggzc.framework.common.util.gy.GyUtils;
import com.css.ggzc.framework.common.util.object.BeanUtils;
import com.css.ggzc.framework.redis.utils.RedisUtils;
import com.css.ggzc.framework.session.SessionUtils;
import com.css.txw.mhzc.mapper.TxwMhzcYhqygxbMapper;
import com.css.txw.mhzc.mapper.TxwMhzcYhxxbMapper;
import com.css.txw.mhzc.pojo.YhxxReqDTO;
import com.css.txw.mhzc.pojo.YhxxbDTO;
import com.css.txw.mhzc.pojo.domain.TxwMhzcQyxxbDO;
import com.css.txw.mhzc.pojo.domain.TxwMhzcYhqygxbDO;
import com.css.txw.mhzc.pojo.domain.TxwMhzcYhxxbDO;
import com.css.txw.mhzc.pojo.vo.SessionVO;
import com.css.txw.mhzc.pojo.vo.UserLockVO;
import com.css.txw.mhzc.pojo.vo.UserRegisterReqVO;
import com.css.txw.mhzc.pojo.vo.UserReqVO;
import com.css.txw.mhzc.pojo.vo.UserVO;
import com.css.txw.mhzc.properties.MhzcProperties;
import com.css.txw.mhzc.service.TxwMhzcQyxxbService;
import com.css.txw.mhzc.service.TxwMhzcYhxxbService;

import cn.hutool.core.util.IdUtil;
import cn.hutool.crypto.digest.MD5;
import lombok.extern.slf4j.Slf4j;

/**
 * @author AirlH
 * @description 针对表【txw_mhzc_yhxxb(用户信息表)】的数据库操作Service实现
 * @createDate 2025-09-08 17:08:11
 */
@Service
@Slf4j
public class TxwMhzcYhxxbServiceImpl extends ServiceImpl<TxwMhzcYhxxbMapper, TxwMhzcYhxxbDO>
        implements TxwMhzcYhxxbService {

    @Resource
    private TxwMhzcYhxxbMapper yhxxbMapper;

    @Resource
    private TxwMhzcYhqygxbMapper yhqygxbMapper;

    @Resource
    private TxwMhzcQyxxbService qyxxbService;
    @Resource
    private MhzcProperties mhzcProperties;

    @Override
    public YhxxbDTO initSessionByYhuuid(YhxxReqDTO reqDTO) {
        TxwMhzcYhxxbDO yhxxbDO = yhxxbMapper.getYhxxByYhuuid(reqDTO.getYhuuid());
        if (GyUtils.isNull(yhxxbDO)) return null;
        YhxxbDTO yhxxbDTO = BeanUtils.toBean(yhxxbDO, YhxxbDTO.class);
        List<TxwMhzcYhqygxbDO> byYhuuid = yhqygxbMapper.getByYhuuid(reqDTO.getYhuuid());
        if (GyUtils.isNull(byYhuuid)) return yhxxbDTO;
        String qyuuid = byYhuuid.get(0).getQyuuid();
        TxwMhzcQyxxbDO qyxxbDO = qyxxbService.getById(qyuuid);
        yhxxbDTO.setQyuuid(qyuuid);
        yhxxbDTO.setQymc(qyxxbDO.getQymc());
        yhxxbDTO.setNsrsbh(qyxxbDO.getNsrsbh());
        return yhxxbDTO;
    }

    @Override
	public YhxxbDTO initSessionByDid(YhxxReqDTO reqDTO) {
    	if (GyUtils.isNull(reqDTO) || GyUtils.isNull(reqDTO.getDid())) return null;
        TxwMhzcYhxxbDO yhxxbDO = yhxxbMapper.getYhxxByDid(reqDTO.getDid());
        if (GyUtils.isNull(yhxxbDO)) return null;
        YhxxbDTO yhxxbDTO = BeanUtils.toBean(yhxxbDO, YhxxbDTO.class);
        List<TxwMhzcYhqygxbDO> byYhuuid = yhqygxbMapper.getByYhuuid(yhxxbDTO.getYhUuid());
        if (GyUtils.isNull(byYhuuid)) return yhxxbDTO;
        String qyuuid = byYhuuid.get(0).getQyuuid();
        TxwMhzcQyxxbDO qyxxbDO = qyxxbService.getById(qyuuid);
        yhxxbDTO.setQyuuid(qyuuid);
        yhxxbDTO.setQymc(qyxxbDO.getQymc());
        yhxxbDTO.setNsrsbh(qyxxbDO.getNsrsbh());
        return yhxxbDTO;
    }

	@Override
	public YhxxbDTO updateDid(YhxxbDTO reqDTO) {
		if (GyUtils.isNull(reqDTO) 
				|| GyUtils.isNull(reqDTO.getDid())
				|| GyUtils.isNull(reqDTO.getYhUuid())
				) return null;
	    log.info("reqDTO:{}",reqDTO);
		LambdaUpdateWrapper<TxwMhzcYhxxbDO> updateWrapper = new LambdaUpdateWrapper<>();
		updateWrapper.set(TxwMhzcYhxxbDO::getDid, reqDTO.getDid())
		.eq(TxwMhzcYhxxbDO::getYhUuid,reqDTO.getYhUuid());
		yhxxbMapper.update(null,updateWrapper);
		return reqDTO;
	}

	@Override
	public YhxxbDTO initSessionBySfzjhm(YhxxReqDTO reqDTO) {
    	if (GyUtils.isNull(reqDTO) || GyUtils.isNull(reqDTO.getSfzjhm())) return null;
        TxwMhzcYhxxbDO yhxxbDO = yhxxbMapper.getYhxxBySfzjhm(reqDTO.getSfzjhm());
        if (GyUtils.isNull(yhxxbDO)) return null;
        YhxxbDTO yhxxbDTO = BeanUtils.toBean(yhxxbDO, YhxxbDTO.class);
        List<TxwMhzcYhqygxbDO> byYhuuid = yhqygxbMapper.getByYhuuid(yhxxbDTO.getYhUuid());
        if (GyUtils.isNull(byYhuuid)) return yhxxbDTO;
        String qyuuid = byYhuuid.get(0).getQyuuid();
        TxwMhzcQyxxbDO qyxxbDO = qyxxbService.getById(qyuuid);
        yhxxbDTO.setQyuuid(qyuuid);
        yhxxbDTO.setQymc(qyxxbDO.getQymc());
        yhxxbDTO.setNsrsbh(qyxxbDO.getNsrsbh());
        return yhxxbDTO;
    }

	@Override
    public YhxxbDTO initSessionByDlzh(YhxxReqDTO reqDTO) {
        TxwMhzcYhxxbDO yhxxbDO = yhxxbMapper.getYhxxByDlzh(reqDTO.getDlzh());
        if (GyUtils.isNull(yhxxbDO)) return null;
        YhxxbDTO yhxxbDTO = BeanUtils.toBean(yhxxbDO, YhxxbDTO.class);
        List<TxwMhzcYhqygxbDO> byYhuuid = yhqygxbMapper.getByYhuuid(reqDTO.getYhuuid());
        if (GyUtils.isNull(byYhuuid)) return yhxxbDTO;
        String qyuuid = byYhuuid.get(0).getQyuuid();
        TxwMhzcQyxxbDO qyxxbDO = qyxxbService.getById(qyuuid);
        yhxxbDTO.setQyuuid(qyuuid);
        yhxxbDTO.setQymc(qyxxbDO.getQymc());
        yhxxbDTO.setNsrsbh(qyxxbDO.getNsrsbh());
        return yhxxbDTO;
    }

    @Override
    public String register(UserRegisterReqVO reqVO) {
        String sms = reqVO.getSms();
        String dlzh = reqVO.getDlzh();
        String s = RedisUtils.get(formatKey(dlzh + sms));
        if (this.mhzcProperties.isRegisterSmsCheck() && (GyUtils.isNull(s) || !dlzh.equals(s))) {
            throw exception(OAUTH2_LOGIN_SJHM_NOT_EXISTS);
        }
        TxwMhzcYhxxbDO yhxxByDlzh = yhxxbMapper.getYhxxByDlzhNoYxbzNoSdbz(dlzh);
        if (!GyUtils.isNull(yhxxByDlzh)) {
            throw exception(YHXX_IS_EXISTS);
        }
        TxwMhzcYhxxbDO yhxxbDO = new TxwMhzcYhxxbDO();
        yhxxbDO.setYhUuid(IdUtil.fastSimpleUUID());
        yhxxbDO.setDlzh(reqVO.getDlzh());
        yhxxbDO.setZsxm1(reqVO.getZsxm1());
        yhxxbDO.setSjhm1(reqVO.getDlzh());
        yhxxbDO.setDlmm(MD5.create().digestHex(reqVO.getPassword()));
        Date now = new Date();
        yhxxbDO.setLrrq(now);
        yhxxbDO.setXgrq(now);
        yhxxbDO.setYxbz("Y");
        yhxxbDO.setSdbz("N");
        yhxxbDO.setYwqdDm("mhzc");
        yhxxbDO.setSjcsdq("mhzc");
        yhxxbDO.setSjgsdq("mhzc");
        yhxxbDO.setLrrsfid("mhzc");
        yhxxbDO.setSjtbSj(now);
        this.save(yhxxbDO);
        return "ok";
    }

    @Override
	public YhxxbDTO saveYhxxByDid(YhxxbDTO reqVO) {
        String dlzh = reqVO.getDlzh();
        TxwMhzcYhxxbDO yhxxByDlzh = yhxxbMapper.getYhxxByDlzhNoYxbzNoSdbz(dlzh);
        if (!GyUtils.isNull(yhxxByDlzh)) {
            throw exception(YHXX_IS_EXISTS);
        }
        TxwMhzcYhxxbDO yhxxbDO = new TxwMhzcYhxxbDO();
        if (GyUtils.isNull(reqVO.getYhUuid())) {
        	 yhxxbDO.setYhUuid(IdUtil.fastSimpleUUID());
		}
        else {
        	 yhxxbDO.setYhUuid(reqVO.getYhUuid());
        }
        yhxxbDO.setDlzh(reqVO.getDlzh());
        yhxxbDO.setZsxm1(reqVO.getZsxm1());
        yhxxbDO.setSjhm1(reqVO.getDlzh());
        yhxxbDO.setDlmm(MD5.create().digestHex("did"+reqVO.getDlzh()));
        yhxxbDO.setSfzjhm(reqVO.getSfzjhm());
        yhxxbDO.setDid(reqVO.getDid());
        Date now = new Date();
        yhxxbDO.setLrrq(now);
        yhxxbDO.setXgrq(now);
        yhxxbDO.setYxbz("Y");
        yhxxbDO.setSdbz("N");
        yhxxbDO.setYwqdDm("did");
        yhxxbDO.setSjcsdq("did");
        yhxxbDO.setSjgsdq("did");
        yhxxbDO.setLrrsfid("did");
        yhxxbDO.setSjtbSj(now);
        this.save(yhxxbDO);
        return reqVO;
	}

	private static String formatKey(String sms) {
        String VERIFY_TOKEN = "sms_token:%s";
        return String.format(VERIFY_TOKEN, sms);
    }

    @Override
    public SessionVO init() {
        SessionVO sessionVO = new SessionVO();
        sessionVO.setYhUuid(SessionUtils.getYhUuid());
        sessionVO.setSjhm1(SessionUtils.getSjhm());
        sessionVO.setZsxm1(SessionUtils.getZsxm());
        List<TxwMhzcYhqygxbDO> list = yhqygxbMapper.getByYhuuid(SessionUtils.getYhUuid());
        if (GyUtils.isNull(list)) return sessionVO;
        TxwMhzcQyxxbDO qyxxbDO = qyxxbService.getById(list.get(0).getQyuuid());
        if (GyUtils.isNull(qyxxbDO)) return sessionVO;
//        sessionVO.setNsrsbh(SessionUtils.getNsrsbh());
//        sessionVO.setQymc((String) SessionUtils.get("qymc"));
        sessionVO.setNsrsbh(qyxxbDO.getNsrsbh());
        sessionVO.setQymc(qyxxbDO.getQymc());
        sessionVO.setQyuuid(qyxxbDO.getQyuuid());
        sessionVO.setGxdtRzbz(qyxxbDO.getGxdtrzbz());
        return sessionVO;
    }

    @Override
    public Page<UserVO> getAllUser(UserReqVO reqVO) {
        Page<TxwMhzcYhxxbDO> page = yhxxbMapper.getAllUser(reqVO);
        if (GyUtils.isNull(page)) return null;
        List<TxwMhzcYhxxbDO> records = page.getRecords();
        List<UserVO> userVOS = BeanUtils.toBean(records, UserVO.class);
        Page<UserVO> resPage = new Page<>();
        resPage.setCurrent(page.getCurrent());
        resPage.setTotal(page.getTotal());
        resPage.setPages(page.getPages());
        resPage.setSize(page.getSize());
        resPage.setRecords(userVOS);
        return resPage;
    }

    @Override
    public String resetPassword(String yhuuid) {
        TxwMhzcYhxxbDO yhxxbDO = new TxwMhzcYhxxbDO();
        yhxxbDO.setYhUuid(yhuuid);
        yhxxbDO.setDlmm(CacheUtils.dm2mc("cs_ggzc_xtcs", "dlmm"));
        this.updateById(yhxxbDO);
        return "success";
    }

    @Override
    public List<String> getAllUserId() {
        List<TxwMhzcYhxxbDO> allUserId = yhxxbMapper.getAllUserId();
        if (GyUtils.isNull(allUserId)) return null;
        return allUserId.stream().map(TxwMhzcYhxxbDO::getYhUuid).collect(Collectors.toList());
    }

    @Override
    public Integer lockUser(UserLockVO infoVO) {
        final Date date = new Date();
        final TxwMhzcYhxxbDO txwMhzcYhxxbDO = new TxwMhzcYhxxbDO();
        txwMhzcYhxxbDO.setYhUuid(infoVO.getYhuuid());
        txwMhzcYhxxbDO.setSdbz("Y");
        txwMhzcYhxxbDO.setSdyy(infoVO.getSdyy());
        txwMhzcYhxxbDO.setSdsj1(date);
        txwMhzcYhxxbDO.setSdryid(SessionUtils.getYhUuid());
        txwMhzcYhxxbDO.setXgrq(date);
        txwMhzcYhxxbDO.setXgrsfid(SessionUtils.getYhUuid());

        return yhxxbMapper.updateById(txwMhzcYhxxbDO);
    }

    @Override
    public Integer unlockUser(UserLockVO infoVO) {
        return yhxxbMapper.unlock(infoVO.getYhuuid(), new Date(), SessionUtils.getYhUuid());
    }
}




