package com.zjrs.ggfw.portal.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 jakarta.annotation.Resource;
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 com.zjrs.ggfw.portal.blo.UserBLO;
import com.zjrs.ggfw.portal.dto.SjdzDetailQueryDTO;
import com.zjrs.ggfw.portal.dto.SjdzQueryDTO;
import com.zjrs.ggfw.portal.dto.SjxxQueryDTO;
import com.zjrs.ggfw.portal.dto.common.PageDTO;
import com.zjrs.ggfw.portal.entity.*;
import com.zjrs.ggfw.portal.po.*;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserBLOImpl extends BLOImpl implements UserBLO {

    @Resource
    VRsWbSjdzMapper vRsWbSjdzMapper;
    
    @Resource
    VRsWbSjxxMapper vRsWbSjxxMapper;
    
    @Resource
    VRsWbSjxxListMapper vRsWbSjxxListMapper;
    
    @Resource
    VRsWbXxglMapper vRsWbXxglMapper;
    
    @Resource
    VRsGgfwUserMapper vRsGgfwUserMapper; 
    
    @Resource
    VAb01Mapper vAb01Mapper;
    
    @Resource
    VAc01Mapper vAc01Mapper;
	
	@Resource
	VRsZjGccrcMapper vRsZjGccrcMapper;
	
	@Resource
	VRsWbYjfkMapper vRsWbYjfkMapper;
	
	@Resource
	VRsZzzdLogMapper vRsZzzdLogMapper;

    @Resource
    VRsGgfwTyrzUserMapper vRsGgfwTyrzUserMapper;

    @Override
    public PageResult getSjdzList(SjdzQueryDTO dto) {
        //查询条件
        QueryWrapper<VRsWbSjdz> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("yhid", dto.getYhid());
        queryWrapper.eq("ts_yxbz", "1");
        
        Page<VRsWbSjdz> page = new Page<>(dto.getPagenum(), dto.getPagesize());
        Page<VRsWbSjdz> pageResult = (Page) this.vRsWbSjdzMapper.getSjdzList(page, dto);
        List<VRsWbSjdz> records = pageResult.getRecords();
        for(VRsWbSjdz record : records){
            if (StringUtils.isNotBlank(record.getTcustProvince())) {
                record.setSjdz(record.getTcustProvince()+record.getSjdz());
            }
        }
        PageResult result = PageUtil.PageResult(pageResult);
        return result;
    }

    @Override
    public VRsWbSjdz getSjdzDetail(SjdzDetailQueryDTO dto) {
        VRsWbSjdz vRsWbSjdz = vRsWbSjdzMapper.getSjdzDetail(dto.getRsWbSjdzLsh());
        return vRsWbSjdz;
    }

    @Override
    public VRsWbSjdz saveSjdz(VRsWbSjdz vRsWbSjdz) {
    	//若为默认地址，则修改原数据为非默认
    	if("1".equals(vRsWbSjdz.getMrdz())) {
    		UpdateWrapper<VRsWbSjdz> updateWrapper = new UpdateWrapper<>();
    		updateWrapper.eq("mrdz", "1");
    		VRsWbSjdz udpRsWbSjdz = new VRsWbSjdz();
    		udpRsWbSjdz.setMrdz("0");
    		
    		vRsWbSjdzMapper.update(udpRsWbSjdz, updateWrapper);
    	}
    	if(StringUtils.isNotBlank(vRsWbSjdz.getRsWbSjdzLsh())) {
    		vRsWbSjdzMapper.updateById(vRsWbSjdz);//更新
    	}
    	return vRsWbSjdz;
    }

    @Override
    public void setMrSjdz(SjdzDetailQueryDTO dto) {
    	VRsWbSjdz vRsWbSjdz = vRsWbSjdzMapper.selectById(dto.getRsWbSjdzLsh());
    	if("1".equals(vRsWbSjdz.getMrdz())) {
    		return;
    	}else {
    		vRsWbSjdz.setMrdz("1");
    	}
    	//还原其他记录的默认标志
    	UpdateWrapper<VRsWbSjdz> updateWrapper = new UpdateWrapper<>();
		updateWrapper.eq("mrdz", "1");
		VRsWbSjdz udpRsWbSjdz = new VRsWbSjdz();
		udpRsWbSjdz.setMrdz("0");
		vRsWbSjdzMapper.update(udpRsWbSjdz, updateWrapper);
		
		//设置默认地址
		vRsWbSjdzMapper.updateById(vRsWbSjdz);
    }
    
    @Override
    public VRsWbSjdz getMrSjdz(String userid) {
    	QueryWrapper<VRsWbSjdz> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("yhid", userid);
        return vRsWbSjdzMapper.selectOne(queryWrapper);
    }

    @Override
    public void deleteSjdz(SjdzDetailQueryDTO dto) {
    	//查询是否默认地址
    	VRsWbSjdz vRsWbSjdz = vRsWbSjdzMapper.selectById(dto.getRsWbSjdzLsh());
    	if("1".equals(vRsWbSjdz.getMrdz())) {
    		//若是，则取最新收件地址为默认
    		QueryWrapper<VRsWbSjdz> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("yhid", vRsWbSjdz.getYhid());           
    	}
    	//设置为无效
    	VRsWbSjdz vRsWbSjdzDel = new VRsWbSjdz();
    	vRsWbSjdzDel.setRsWbSjdzLsh(dto.getRsWbSjdzLsh());
    	vRsWbSjdzDel.setTsyxbz("0");
    	vRsWbSjdzMapper.updateById(vRsWbSjdzDel);
    }
    
    @Override
    public int getNewsCnt(String userid) {
    	QueryWrapper<VRsWbSjxx> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("ydzt", "0");
        queryWrapper.eq("ts_yxbz", "1");
        return vRsWbSjxxMapper.selectCount(queryWrapper);
        
    }
    
    @Override
    public PageResult getNewsList(SjxxQueryDTO dto) {
    	//查询条件
        QueryWrapper<VRsWbSjxxList> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("sjrid", dto.getUserid());
        
        Page<VRsWbSjxxList> page = new Page<>(dto.getPagenum(), dto.getPagesize());
        Page<VRsWbSjxxList> pageResult = (Page) this.vRsWbSjxxListMapper.selectPage(page, queryWrapper);
        PageResult result = PageUtil.PageResult(pageResult);
        return result;
    }
    
    @Override
    public void saveNews(VRsWbXxgl vRsWbXxgl) {
    	vRsWbXxglMapper.insert(vRsWbXxgl);
    }
    
    @Override
    public void saveNewsSj(VRsWbSjxx vRsWbSjxx) {
    	vRsWbSjxxMapper.insert(vRsWbSjxx);
    }
    
    @Override
    public void setNewsZt(VRsWbSjxx vRsWbSjxx) {
    	vRsWbSjxxMapper.updateById(vRsWbSjxx);
    }
    
    @Override
    public void setNewsClear(String userid) {
    	UpdateWrapper<VRsWbSjxx> updateWrapper = new UpdateWrapper<>();
		updateWrapper.eq("ts_yxbz", "0");
		VRsWbSjxx udpRsWbSjxx = new VRsWbSjxx();
		udpRsWbSjxx.setTsyxbz("2");//从回收站删除
		vRsWbSjxxMapper.update(udpRsWbSjxx, updateWrapper);
    }
    
    @Override
    public VRsGgfwUser getVRsGgfwUser(String userid) {
    	return vRsGgfwUserMapper.selectById(userid);
    }
    
    @Override
    public void updateRsGgfwUser(VRsGgfwUser vRsGgfwUser) {
    	vRsGgfwUserMapper.updateById(vRsGgfwUser);
    }
    
    @Override
    public VAb01 getAb01ById(String ztid) {
    	return vAb01Mapper.selectById(ztid);
    }
    
    @Override
    public VAb01 getAb01ByAab998OrAab004(String aab998,String aab004) {
    	VAb01 vAb01 = null;
    	QueryWrapper<VAb01> queryWrapper = new QueryWrapper<>();
    	//若统一社会信用代码未匹配到数据，则用单位名称匹配
    	if(vAb01 == null && StringUtils.isNotBlank(aab004)) {
    		queryWrapper = new QueryWrapper<>();
    		queryWrapper.eq("aab004", aab004);
    		vAb01 = vAb01Mapper.selectOne(queryWrapper);
    	}
    	return vAb01;
    }
    
    @Override
    public VAc01 getAc01ById(String ztid) {
    	return vAc01Mapper.selectById(ztid);
    }
    
    @Override
    public VAc01 getAc01ByZj(String aac058,String aac147) {
    	QueryWrapper<VAc01> queryWrapper = new QueryWrapper<>();
    	queryWrapper.eq("aac058", aac058);
    	return vAc01Mapper.selectOne(queryWrapper);
    	
    }
    
    @Override
    public void saveAb01(VAb01 vAb01) {
    	vAb01Mapper.insert(vAb01);
    }
    
    @Override
    public void saveAc01(VAc01 vAc01) {
    	vAc01Mapper.insert(vAc01);
    }
    
    @Override
    public VRsZjGccrc getGccrc(String ztid) {
    	QueryWrapper<VRsZjGccrc> queryWrapper = new QueryWrapper<>();
    	queryWrapper.eq("ztid", ztid);
    	return vRsZjGccrcMapper.selectOne(queryWrapper);
    }
    
    @Override
    public void saveRsWbYjfk(VRsWbYjfk VRsWbYjfk) {
    	vRsWbYjfkMapper.insert(VRsWbYjfk);
    }
    
    @Override
    public void saveRsZzzdLog(VRsZzzdLog vRsZzzdLog) {
    	vRsZzzdLogMapper.insert(vRsZzzdLog);
    }
    
    @Override
    public PageResult getRsWbYjfkList(String userid,PageDTO dto) {
    	//查询条件
        QueryWrapper<VRsWbYjfk> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("fkr", userid);
        
        Page<VRsWbYjfk> page = new Page<>(dto.getPagenum(), dto.getPagesize());
        Page<VRsWbYjfk> pageResult = (Page) this.vRsWbYjfkMapper.selectPage(page, queryWrapper);
        PageResult result = PageUtil.PageResult(pageResult);
        return result;
    }
    
    @Override
    public VRsWbYjfk getRsWbYjfkById(String rsWbYjfkLsh) {
    	return vRsWbYjfkMapper.selectById(rsWbYjfkLsh);
    }

    @Override
    public String getTyrzPhoneNumber(String userid){
        return vRsGgfwTyrzUserMapper.getTELEPHONENUMBER(userid);
    }
}
