package com.ruoyi.web.controller.Ytj;

import com.ruoyi.common.config.Global;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.WebSocketServer;
import com.ruoyi.system.domain.HyDictionary;
import com.ruoyi.system.domain.QyCzrxx;
import com.ruoyi.system.domain.QyInfo;
import com.ruoyi.system.domain.QyPerson;
import com.ruoyi.system.service.IQyCzrxxService;
import com.ruoyi.system.service.IQyInfoService;
import com.ruoyi.system.service.IQyPersonService;
import com.ruoyi.system.service.IYtjService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.*;

import static com.ruoyi.common.utils.ImgUtils.Base64ToImage;

/**
 *    企业控股
 */
@Controller
@RequestMapping("/Ytj/qygskg")
public class QygskgController {

    private String prefix = "Ytj/qygskg";


    @Autowired
    private IQyPersonService qyPersonService;
    @Autowired
    private IYtjService ytjService;
    @Autowired
    private IQyInfoService qyInfoService;
    @Autowired
    private IQyCzrxxService qyCzrxxService;


    /**
     * 打开功能列表
     *
     * @return 页面
     */
    @GetMapping()
    public String index(ModelMap modelMap) {
        modelMap.put("city",Global.getCityName());
        return "Ytj/qyIndex";
    }


    /**
     * 打开刷身份证页面
     *
     * @return 页面
     */
    @GetMapping("/read")
    public String read(Model model) {
        model.addAttribute("city", Global.getCityName());

        return prefix + "/RCard";
    }

    /**
     * 读取采集信息
     *
     * @return
     */
    @GetMapping("/caiji")
    public String caiji( Model model) {

        String Bsid = UUID.randomUUID().toString();
        model.addAttribute("Bsid", Bsid);
        model.addAttribute("city", Global.getCityName());
        model.addAttribute("rzdbsocre", Global.getConfig("ruoyi.rzdbsocre"));
        return prefix + "/Index";
    }

    /**
     * 提交身份证信息
     * @param qyPerson
     * @return
     */
    @PostMapping("/SubmitCard")
    @ResponseBody
    public AjaxResult saveCard(QyPerson qyPerson, @RequestParam("IsHere") String ishere) {

        qyPerson.setAddtime(new Date());

        String pzPath= "/QySqImg/"+ DateUtils.datePaths()+"/" +UUID.randomUUID().toString().replaceAll("-","").toLowerCase()+".jpg";
        String qmPath= "/QySqImg/"+ DateUtils.datePaths()+"/" +UUID.randomUUID().toString().replaceAll("-","").toLowerCase()+".jpg";
        String txPath= "/QySqImg/"+ DateUtils.datePaths()+"/" +UUID.randomUUID().toString().replaceAll("-","").toLowerCase()+".jpg";
        if(Base64ToImage(qyPerson.getPaiphoto(),Global.getProfile()+pzPath)){
            qyPerson.setPaiphoto("/profile"+pzPath);
        }
        if(Base64ToImage(qyPerson.getRenqianming(),Global.getProfile()+qmPath)){
            qyPerson.setRenqianming("/profile"+qmPath);
        }
        if(Base64ToImage(qyPerson.getRenphoto(),Global.getProfile()+txPath)){
            qyPerson.setRenphoto("/profile"+txPath);
        }

        int result=0;
        if(ishere.equals("0")) {
            result = qyPersonService.insertQyPerson(qyPerson);
        }else{
            result=qyPersonService.updateWtr(qyPerson);
        }
        if (result == 1) {
            return AjaxResult.success("保存成功");
        }
        return AjaxResult.warn("保存失败,请重试");
    }


    /**
     *
     * //材料类别(根据行业大类获得)
     *
     * @return
     */
    @GetMapping("/GetCailiao")
    @ResponseBody
    public List<Map> GetCailiao(@RequestParam("hyid") String hyid, Model model) {

        List<Map> resul = qyPersonService.GetCailiao(hyid);
        if (resul.size() > 0) {
            return resul;
        }
        return null;
    }

    /**
     * 查询行政区划
     * @return
     */
    @GetMapping(value="/GetQh")
    @ResponseBody
    public List<Map> QhList() {
        List<Map> qh = ytjService.GetQh();
        return qh;
    }

    /**
     * 查找行业分类
     * @return
     * @throws UnsupportedEncodingException
     */
    @GetMapping(value="/GetHy")
    @ResponseBody
    public List<Map> GetHy() throws UnsupportedEncodingException {
        List<Map> map = ytjService.GetHy();
        return map;
    }

    /**
     * 根据pid查找区划
     * @param pid
     * @return
     * @throws UnsupportedEncodingException
     */
    @GetMapping(value="/GetQu")
    @ResponseBody
    public List<Map> GetQu(String pid) throws UnsupportedEncodingException {
        List<Map> map = ytjService.GetQu(pid);
        return map;
    }

    /**
     * 查找行业分类子级
     * @param pid
     * @return
     * @throws UnsupportedEncodingException
     */
    @GetMapping(value="/GetChildHy")
    @ResponseBody
    public List<HyDictionary> GetChildHy(@RequestParam("pid") String pid) throws UnsupportedEncodingException {
        System.err.println(pid);
        List<HyDictionary> map = ytjService.GetChildHy(pid);
        return map;
    }

    /**
     * 提交正反面信息
     *
     * @return
     */
    @PostMapping("/SendCailiao_Wtr")
    @ResponseBody
    public AjaxResult SendCailiao_Wtr(HttpServletRequest request) {
        String bsid = request.getParameter("bsid");
        String clname = request.getParameter("clname");
        String clindex = request.getParameter("clindex");
        String clsrc = request.getParameter("clsrc");
        String imgPath= "/QySqImg/"+ DateUtils.datePaths()+"/" +UUID.randomUUID().toString().replaceAll("-","").toLowerCase()+".jpg";
        if(Base64ToImage(clsrc,Global.getProfile()+imgPath)){
            clsrc="/profile"+imgPath;
        }
        QyPerson qyPerson=new QyPerson();
        qyPerson.setBsid(bsid);
        //qyPerson.setCzrsfz(clindex);
        int result;
        if (clindex.equals("1")) {
            qyPerson.setCzrsfz(clsrc);
            result=qyPersonService.updateWtr(qyPerson);
        }else if(clindex.equals("2")){
            qyPerson.setCzrsfz1(clsrc);
            result=qyPersonService.updateWtr(qyPerson);
        }else {
            result = -1;
        }
        System.err.println("res:"+result);
        if (result> 0) {
            return AjaxResult.success("保存成功");
        }
        return AjaxResult.warn("保存错误,请勿多拍!");
    }

    /**
     * 返回登记信息界面
     * @param
     * @param bsid
     * @return
     */
    @GetMapping("/Jbxx")
    public String jbxx(@RequestParam("BSID")String bsid,Model model) {

        model.addAttribute("city",Global.getCityName());
        return prefix+"/Jbxx";

    }

    /**
     * 返回登记信息界面
     * @param
     * @param bsid
     * @return
     */
    @GetMapping("/JbxxUp")
    public String JbxxUp(@RequestParam("BSID")String bsid,Model model) {

        QyInfo info=qyInfoService.selectQyInfoByBsId(bsid);
            model.addAttribute("info", info);
        model.addAttribute("city", Global.getCityName());
        return prefix+"/Jbxx";

    }


    /**
     * 提交企业信息
     *
     */
    @PostMapping("/SubmitForm")
    @ResponseBody
    public AjaxResult submitFrom(HttpServletRequest request){

        int result=qyInfoService.submitFrom(request);
        return AjaxResult.success(result);
    }

    /**
     * 返回登记信息界面
     * @param
     * @param bsid
     * @return
     */
    @GetMapping("/Ryxinxi")
    public String Ryxinxi(@RequestParam("bsid")String bsid, @RequestParam("qytype")String qytype, Model model) {
        model.addAttribute("city", Global.getCityName());
        return prefix+"/Ryxinxi";
    }

    /**
     * 提交企业信息
     *
     */
    @GetMapping("/getWtrxinxi")
    @ResponseBody
    public AjaxResult getWtrxinxi(@RequestParam("bsid") String bsid , @RequestParam("sjs")String sjs,Model model){
        List<QyPerson> result=qyPersonService.selectQyPersonBybsid(bsid);
        QyCzrxx czr=new QyCzrxx();
        czr.setBsid(bsid);
        QyInfo info=qyInfoService.selectQyInfoByBsId(bsid);
        List<QyCzrxx> czrx=qyCzrxxService.selectQyCzrxxList(czr);
        Map map=new HashMap();
        map.put("wtr",result);
        map.put("czr",czrx);
        map.put("info",info);
        return AjaxResult.success("查到委托人信息",map);
    }

    /**
     * 读取身份证人员信息
     *
     * @return
     */
    @GetMapping("/shuaCard")
    public String shuaCard(@RequestParam("bsid") String bsid,@RequestParam("rytype") String rytype,Model model) {
        model.addAttribute("city", Global.getCityName());
        return prefix + "/shuaCard";
    }

    /**
     * 人员信息录取页面
     *
     * @return
     */
    @GetMapping("/ryPaizhao")
    public String ryPaizhao(@RequestParam("bsid") String bsid,@RequestParam("rytype") String rytype,Model model) {
        model.addAttribute("city", Global.getCityName());
        model.addAttribute("rzdbsocre", Global.getConfig("ruoyi.rzdbsocre"));
        return prefix + "/ryPaizhao";
    }

    /**
     * 人员信息录取表单提交
     *
     * @return
     * */
    @PostMapping("/submitCzr")
    @ResponseBody
    public AjaxResult ryPaizhao(HttpServletRequest request) {


        int i = qyCzrxxService.insertQyCzr(request);
        if (i == 1) {
            return AjaxResult.success("出资人信息保存成功");
        }
        return AjaxResult.warn("出资人信息保存失败");
    }

    /**
     * 人员信息录取表单提交
     *
     * @return
     * */
    @PostMapping("/submitRy")
    @ResponseBody
    public AjaxResult ryPaizhao(QyPerson qyPerson) {
        QyInfo info= new QyInfo();
        if(qyPerson.getRentype().equals("法定代表人")){
            qyPerson.setIsfr("1");
            info.setBsid(qyPerson.getBsid());
            qyPerson.setCsfs("选举");
            info.setFrname(qyPerson.getRenname());
            qyInfoService.submitInfo(info);
        }
        if(qyPerson.getRentype().equals("联络员")){
            System.err.println(qyPerson.getRenmoblie());
            info.setBsid(qyPerson.getBsid());
            info.setQylxdh(qyPerson.getRenmoblie());
            qyInfoService.submitInfo(info);
        }
        if(qyPerson.getRentype().equals("监事")){
            qyPerson.setCsfs("选举");
        }else if(qyPerson.getRentype().equals("经理")){
            qyPerson.setCsfs("聘任");
        }else if(qyPerson.getRentype().equals("董事")){
            qyPerson.setCsfs("选举");
        }
        //自拍照
        String pzPath= "/QySqImg/"+ DateUtils.datePaths()+"/" +UUID.randomUUID().toString().replaceAll("-","").toLowerCase()+".jpg";
        //签名
        String qmPath= "/QySqImg/"+ DateUtils.datePaths()+"/" +UUID.randomUUID().toString().replaceAll("-","").toLowerCase()+".jpg";
        //身份证头像
        String txPath= "/QySqImg/"+ DateUtils.datePaths()+"/" +UUID.randomUUID().toString().replaceAll("-","").toLowerCase()+".jpg";
        //身份证0
        String czrsfz= "/QySqImg/"+ DateUtils.datePaths()+"/" +UUID.randomUUID().toString().replaceAll("-","").toLowerCase()+".jpg";
        //身份证1
        String czrsfz1= "/QySqImg/"+ DateUtils.datePaths()+"/" +UUID.randomUUID().toString().replaceAll("-","").toLowerCase()+".jpg";
        if(Base64ToImage(qyPerson.getPaiphoto(),Global.getProfile()+pzPath)){
            qyPerson.setPaiphoto("/profile"+pzPath);
        }
        if(Base64ToImage(qyPerson.getRenqianming(),Global.getProfile()+qmPath)){
            qyPerson.setRenqianming("/profile"+qmPath);
        }
        if(Base64ToImage(qyPerson.getRenphoto(),Global.getProfile()+txPath)){
            qyPerson.setRenphoto("/profile"+txPath);
        }
        if(Base64ToImage(qyPerson.getCzrsfz1(),Global.getProfile()+czrsfz1)){
            qyPerson.setCzrsfz1("/profile"+czrsfz1);
        }
        if(Base64ToImage(qyPerson.getCzrsfz(),Global.getProfile()+czrsfz)){
            qyPerson.setCzrsfz("/profile"+czrsfz);
        }
        int i = qyPersonService.insertQyPerson(qyPerson);
        if (i == 1) {
            return AjaxResult.success("保存成功");
        }
        return AjaxResult.warn("信息保存失败");
    }


    /**
     * 采集信息删除
     *
     * @return
     * */
    @PostMapping("/DelRY")
    @ResponseBody
    public int caijiDel(@RequestParam("bsid")String bsid,@RequestParam("ryid")Long ryid, @RequestParam("type") int type) {

        if(type==0){
            return qyCzrxxService.deleteQyCzrxxById(ryid);
        }else if(type==1){
            return qyPersonService.deleteQyPersonById(ryid);
        }else {
            return -1;
        }
    }
    /**
     *
     * @param bsid
     * @return
     */
    @GetMapping("/getQrtjxinxi")
    @ResponseBody
    public AjaxResult getQrtjxinxi(@RequestParam("bsid") String bsid ,Model model) {

        QyInfo result=qyInfoService.selectQyInfoByBsId(bsid);
        if(result==null){
            return AjaxResult.warn("不存在的企业信息");
        }
        return AjaxResult.success("企业信息",result);
    }

    /**
     * 人员信息录取表单提交
     *
     * @return
     * */
    @PostMapping("/SubmitTj")
    @ResponseBody
    public AjaxResult SubmitTj(@RequestParam("bsid")String bsid) {

        Date carteTime=new Date();
        QyInfo info= qyInfoService.selectQyInfoByBsId(bsid);
        info.setIsprintZ("0");
        info.setIsprintF("0");
        info.setFlg("0");
        info.setCreatedate(carteTime);
        info.setLqdate(carteTime);
        int i= qyInfoService.submitInfo(info);
        if (i == 1) {
            try {
                WebSocketServer.sendInfo("企业控股："+ info.getFrname() +"的申请消息!请及时查办","0");
                //flg代表着企业审核状态
                int flg=0;
                qyInfoService.sendMessage(info.getFrname(),info.getQylxdh(),info.getQyname(),"",flg);
            } catch (IOException e) {
                e.printStackTrace();
                System.err.println(e);
            }
            return AjaxResult.success("出资人信息保存成功");
        }
        return AjaxResult.warn("出资人信息保存失败");
    }

    /**
     *  返回成功页面
     * @return
     */
    @GetMapping("/sl6")
    public String successSum(Model model) {
        model.addAttribute("city", Global.getCityName());
        return prefix+"/sl6";
    }

    /**
     * 遍历已经录入的人员信息
     * @param bsid
     * @return
     */
    @GetMapping("/getperson")
    @ResponseBody
    public AjaxResult getperson(@RequestParam("bsid")String bsid){

        List<QyPerson> qyPeople=qyPersonService.selectQyPersonBybsids(bsid);
        return AjaxResult.success("查找成功:", qyPeople);
    }

    /**
     * 快捷选择人员
     * @param id
     * @return
     */
    @GetMapping("/setperson")
    @ResponseBody
    public AjaxResult setperson( @RequestParam("id")Long id,@RequestParam("typeid") Integer typeid){

        QyPerson qyPeople=qyPersonService.selectQyPersonById(id);

        if (typeid == 0) {
            return  AjaxResult.warn("错误的选择");
        }
        qyPeople.setId(null);
        if(typeid==1){
            qyPeople.setRentype("法定代表人");
            qyPeople.setIsfr("1");
            qyPeople.setCsfs("选举");
            QyInfo info= new QyInfo();
            info.setBsid(qyPeople.getBsid());
            info.setFrname(qyPeople.getRenname());
            qyInfoService.submitInfo(info);
        }else if(typeid==2){
            qyPeople.setRentype("董事");
            qyPeople.setCsfs("选举");
        }else if(typeid==3){
            qyPeople.setRentype("监事");
            qyPeople.setCsfs("选举");
        }else if(typeid==4){
            qyPeople.setRentype("经理");
            qyPeople.setCsfs("聘任");
        }else if(typeid==5){
            qyPeople.setRentype("财务负责人");
        }else if(typeid==6){
            qyPeople.setRentype("联络员");
        }
        qyPersonService.insertQyPerson(qyPeople);
        return AjaxResult.success("查找成功:", qyPeople);
    }

    /**
     * 获取到 保存的信息
     * @param card
     * @return
     */
    @GetMapping("/qyxinxi")
    @ResponseBody
    public AjaxResult qyxinxi (@RequestParam("card") String card){
        List<QyInfo> infos= qyInfoService.selectQyInfoByCard(card);
        if(infos.size()>0) {
            for (QyInfo in : infos) {
                if(in.getFlg()==null&&in.getQylx().equals("有限责任公司（自然人投资或控股）")) {
                return AjaxResult.success("未进行完的信息",in);
                }
            }
        }
        return AjaxResult.warn("暂无信息");
    }

}














