package com.chinaweal.aiccs.aiceps.annual.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.chinaweal.aiccs.aiccs.abnormal.entity.EBaseinfo;
import com.chinaweal.aiccs.aiccs.abnormal.service.EBaseinfoService;
import com.chinaweal.aiccs.aiccs.annual.entity.EPubObjection;
import com.chinaweal.aiccs.aiceps.annual.entity.AccountPwdLogon;
import com.chinaweal.aiccs.aiceps.annual.service.NoAnnualService;
import com.chinaweal.aiccs.common.base.controller.BaseController;
import com.chinaweal.aiccs.common.constant.BaseDataConstant;
import com.chinaweal.aiccs.common.util.RSAUTil;
import com.chinaweal.aiccs.common.util.StringUtils;
import com.chinaweal.youfool.framework.springboot.mybatis.plus.page.PageRequestDto;
import com.chinaweal.youfool.framework.springboot.rest.RestResult;
import com.chinaweal.youfool.framework.springboot.rest.ResultCode;
import com.chinaweal.youfool.framework.springboot.util.AssertUtils;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * <p>
 * 年报数据获取 前端控制器
 * </p>
 *
 * @author lwd
 * @since 2021-07-20
 */
@ApiOperation("年报数据获取接口")
@RestController
@RequestMapping("/annualAiceps")
public class NoAnnualController extends BaseController {

    @Autowired
    private NoAnnualService noAnnualService;

    @Autowired
    private EBaseinfoService eBaseinfoService;

    /**
     * 获取年报账号密码
     * @return  获取结果
     * @author lwd
     * @since  2022年4月27日 10:09:38
     */
    @ApiOperation("通过企业信息获取账号信息")
    @PostMapping("getAnnualAccount")
    public RestResult<?> getAnnualAccount( @RequestBody PageRequestDto pageRequestDto){


        Map<String, Object> result = new HashMap<>();
        LambdaQueryWrapper<EBaseinfo> lambdaQueryWrapper = new LambdaQueryWrapper();
        String uniscId = String.valueOf(pageRequestDto.getCustomParamMap().get("searchUscc"));
        String entName = String.valueOf(pageRequestDto.getCustomParamMap().get("searchName"));
        String regNo = String.valueOf(pageRequestDto.getCustomParamMap().get("searchRegno"));
        if (StringUtils.isNotBlank(uniscId)){
            lambdaQueryWrapper.eq(EBaseinfo::getUniscid,uniscId);
        }
        if (StringUtils.isNotBlank(entName)){
            lambdaQueryWrapper.eq(EBaseinfo::getEntname,entName);
        }
        if (StringUtils.isNotBlank(regNo)){
            lambdaQueryWrapper.eq(EBaseinfo::getRegno,regNo);
        }
        //查出企业信息
        lambdaQueryWrapper.eq(EBaseinfo::getRegstate,"1");
        EBaseinfo eBaseinfo = eBaseinfoService.getOne(lambdaQueryWrapper);
        if (eBaseinfo == null)return RestResult.error(ResultCode.DATA_NONE);

        //根据企业主键查相关账号信息
        List<AccountPwdLogon> accountPwdLogonlist = noAnnualService.selectAnnualAccountByPripid(eBaseinfo.getPripid());
        List<AccountPwdLogon> accountPwdLogonNewlist = new ArrayList<>();
        if (accountPwdLogonlist.size()>0){
            for (int i = 0; i < accountPwdLogonlist.size(); i++) {
                String enpwd = accountPwdLogonlist.get(i).getSPWD().substring(8,accountPwdLogonlist.get(i).getSPWD().length());
                try {
                    String jmpwd = RSAUTil.decrypt(enpwd, BaseDataConstant.privateKey);
                    accountPwdLogonlist.get(i).setSPWD(jmpwd);
                    accountPwdLogonNewlist.add(accountPwdLogonlist.get(i));
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            result.put("accountPwdLogonNewlist",accountPwdLogonNewlist);

        }else {
            return RestResult.error(ResultCode.DATA_NONE);
        }


        return RestResult.ok(result);
    }

}
