package com.pj.tb_cooperative;

import cn.hutool.core.util.RandomUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pj.api.client.admin.AdminInterface;
import com.pj.api.consts.FeignFactory;
import com.pj.api.dto.AdminCreateDto;
import com.pj.api.dto.CooperativeDto;
import com.pj.api.dto.HtCooperativeDto;
import com.pj.current.config.SystemObject;
import com.pj.current.satoken.StpUserUtil;
import com.pj.enummj.DeleteStatus;
import com.pj.tb_trade_area.TbTradeArea;
import com.pj.tb_trade_area.TbTradeAreaService;
import com.pj.utils.so.SoMap;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * Service: tb_cooperative -- 合作社
 *
 * @author lsw
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class TbCooperativeService extends ServiceImpl<TbCooperativeMapper, TbCooperative> implements IService<TbCooperative> {

    /**
     * 底层 Mapper 对象
     */
    @Autowired
    TbCooperativeMapper tbCooperativeMapper;
    @Resource
    private TbTradeAreaService tbTradeAreaService;
    @Autowired
    private AdminInterface adminInterface;

    /**
     * 增
     */
    void add(TbCooperative t) {
        t.setCreateTime(new Date());
        t.setCreateName(StpUserUtil.getPCLoginInfo().getLoginName());
        save(t);
    }

    /**
     * 删
     */
    void delete(Long id) {
        removeById(id);
    }

    /**
     * 改
     */
    void update(TbCooperative t) {
        t.setUpdateTime(new Date());
        t.setUpdateName(StpUserUtil.getPCLoginInfo().getLoginName());
        updateById(t);

    }

    /**
     * 查
     */
    TbCooperative getById(Long id) {
        return super.getById(id);
    }

    /**
     * 查集合 - 根据条件（参数为空时代表忽略指定条件）
     */
    List<TbCooperative> getList(SoMap so) {
        return tbCooperativeMapper.getList(so);
    }


    /*------            正常业务👆👆    rpc远程调用👇👇                         -----------*/

    public CooperativeDto getCooperativeById(Long id) {
        TbCooperative cooperative = tbCooperativeMapper.selectById(id);
        CooperativeDto dto = new CooperativeDto();
        BeanUtils.copyProperties(cooperative, dto);
        return dto;
    }

    /**
     * 远程调用: 对合作社信息进行事务处理
     */
    public boolean cooperativeDto(HtCooperativeDto cooperativeDto) {
        TbCooperative tbCooperative = new TbCooperative();
        BeanUtils.copyProperties(cooperativeDto, tbCooperative);
        tbCooperative.setDeleteStatus(DeleteStatus.DELETE_STATUS_ON.getCode());
        TbTradeArea tbTradeArea = tbTradeAreaService.updateHandler(cooperativeDto.getFieldCode(), "", "");
        tbCooperative.setTradeAreaId(tbTradeArea.getId()).setTradeAreaName(tbTradeArea.getName());
        //查询是否有数据
        List<TbCooperative> tbCooperativeList = tbCooperativeMapper
                .selectList(new LambdaQueryWrapper<TbCooperative>().eq(TbCooperative::getOrgSccd, cooperativeDto.getOrgSccd()));
        if (tbCooperativeList.size() == 0) {
            tbCooperative.setCreateTime(new Date());
            //新增
            boolean save = this.save(tbCooperative);
            //新增的同时给合作社代表创建一个登陆后台的管理账号
            if(save){
                //随机生成6位密码
                String code = RandomUtil.randomNumbers(6);
                SystemObject.smsFactory.handler().sendSMS(cooperativeDto.getCorrepTel(), "您的密码是" + code + "。请及时修改密码");
                AdminCreateDto dto=new AdminCreateDto();
                dto.setTradeAreaId(tbTradeArea.getId());
                dto.setPassword(code);
                dto.setName(cooperativeDto.getCorrepTel());
                dto.setRoleId(99); //所属角色ID，当前是99
                dto.setType("01");//类型(00=运营管理者,01=合作社)
                adminInterface.createAdmin(dto);
            }
            return save;
        }
        tbCooperative.setName(cooperativeDto.getCorrepName());
        //修改
        BeanUtils.copyProperties(tbCooperativeList.get(0), tbCooperative);
        int i = tbCooperativeMapper.updateById(tbCooperative);
        return i == 1;
    }

    /**
     * 合作社保存银行卡信息
     */
    public boolean saveBankByCooperative(Long id, String bankName, String bankAccount) {
        LambdaUpdateWrapper<TbCooperative> wrapper = new LambdaUpdateWrapper<>();
        wrapper.set(TbCooperative::getBankName, bankName);
        wrapper.set(TbCooperative::getBankAccount, bankAccount);
        wrapper.eq(TbCooperative::getId, id);

        return update(wrapper);
    }

}
