package com.zcloud.controller.app;

import com.zcloud.controller.base.BaseController;
import com.zcloud.entity.Page;
import com.zcloud.entity.PageData;
import com.zcloud.service.commitment.CorpPromiseDetailsService;
import com.zcloud.service.commitment.CorpPromisePeopleService;
import com.zcloud.service.commitment.CorpPromiseService;
import com.zcloud.service.system.UsersService;
import com.zcloud.util.Const;
import com.zcloud.util.DateUtil;
import com.zcloud.util.Smb;
import com.zcloud.util.Tools;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

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

/**
 * 说明：手机承诺书表
 * 作者：yangming
 * 时间：2022-12-29
 */
@Controller
@RequestMapping("/app/corppromise")
public class AppPromisePart_IIController extends BaseController {

    @Resource
    private CorpPromisePeopleService corpPromisePeopleService;
    @Resource
    private CorpPromiseService corpPromiseService;
    @Resource
    private CorpPromiseDetailsService corpPromiseDetailsService;

    @Resource
    private UsersService usersService;
    @Resource
    private Smb smb;


    /**
     * 是否签字
     *
     * @param
     * @throws Exception
     */
    @RequestMapping(value = "/ISSIGN_II")
    @ResponseBody
    public Object list() throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();
        String errInfo = "success";
        PageData pd = new PageData();
        pd = this.getPageData();
        List<PageData> varList = corpPromisePeopleService.findByUserId(pd);    //查询是否有未签字数据
        if (varList.size() > 0) {
            map.put("ISSIGN", 1);
        } else {
            map.put("ISSIGN", 0);
        }
        map.put("result", errInfo);
        return map;
    }

    /**
     * 需要承诺人签字的承诺书（正文、副文）
     *
     * @param
     * @throws Exception
     */
    @RequestMapping(value = "/promise_II")
    @ResponseBody
    public Object promise() throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();
        String errInfo = "success";
        PageData pd = new PageData();
        pd = this.getPageData();
        List<PageData> varList = corpPromisePeopleService.findByUserId(pd);    //查询是否有未签字数据
        if (varList.size() > 0) {
            pd.put("PROMISE_ID", varList.get(0).getString("PROMISE_ID"));
            PageData TEXT = corpPromiseService.findById(pd);
            List<PageData> COLLATERAL = corpPromiseDetailsService.findByPromiseId(pd);
            List<PageData> COVERPEOPLE = corpPromisePeopleService.findByCoverPromiseId(pd);  //被承诺人

            map.put("TEXT", TEXT);
            map.put("COLLATERAL", COLLATERAL);
            map.put("COVERPEOPLE", COVERPEOPLE);
            map.put("PROMISEPEOPLE_ID", varList.get(0).getString("PROMISEPEOPLE_ID"));  //当前承诺人数据
        }
        map.put("result", errInfo);
        return map;
    }

    /**
     * 保存承诺人签字图片和日期
     *
     * @param
     * @throws Exception
     */
    @RequestMapping(value = "/editpeople_II")
    @ResponseBody
    public Object editpeople(@RequestParam(value = "FFILE", required = false) MultipartFile file) throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();
        String errInfo = "success";
        PageData pd = new PageData();
        pd = this.getPageData();
        String ffile = DateUtil.getDays();
        if (file != null) {
            String suffixName = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf(".") + 1).toLowerCase();
            if (!"pdf".equals(suffixName) && !"jpg".equals(suffixName) && !"jpeg".equals(suffixName) && !"png".equals(suffixName) && !"mp4".equals(suffixName)) {
                errInfo = "fail";
                map.put("result", errInfo);
                map.put("msg", "文件格式不正确！");
                return map;
            }
            String fileName = this.get32UUID() + file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
            smb.sshSftp(file, fileName, Const.FILEPATHFILE + pd.getString("CORPINFO_ID") + "/" + ffile);
            pd.put("FILEPATH", Const.FILEPATHFILE + pd.getString("CORPINFO_ID") + "/" + ffile + "/" + fileName);
            pd.put("SIGNTIME", pd.getString("SIGNTIME"));
            pd.put("ISSIGN", 1);
            pd.put("ISREAD", "0");
            pd.put("SIGNTIME", DateUtil.date2Str(new Date()));
            corpPromisePeopleService.editSign(pd);   //存承诺书签字图片信息
            PageData promise = corpPromisePeopleService.findById(pd);  //查询出承诺书ID
            pd.put("PROMISE_ID", promise.getString("PROMISE_ID"));
            List<PageData> cover = corpPromisePeopleService.findByCoverPromiseId(pd);  //获取被承诺人数据
            pd.put("USER_ID", cover.get(0).getString("USER_ID"));
            PageData user = usersService.findById(pd);
            if (user != null) {
                PageData mes = new PageData();
                mes.put("BIANMA", "PROMISE_001");
                mes.put("SENDER_ID", promise.getString("USER_ID"));    // 发送人员ID
                mes.put("SENDER_NAME", promise.getString("USER_ID"));    // 发送人员姓名
                mes.put("SYNOPSIS", "友情提示：有一条承诺书需要查看");    // 站内信标题
                mes.put("CORPINFO_ID", pd.get("CORPINFO_ID"));// 企业id
                mes.put("RECEIVER_ID", cover.get(0).getString("USER_ID"));    // 接收人员ID
                PageData content = new PageData();
                content.put("params", "我的-安全承诺-接收承诺列表");// 作业操作
                mes.put("CONTENT", content);// 站内信内容
            } else {
                if (Tools.notEmpty(cover.get(0).getString("USER_ID"))) {
                    PageData mes = new PageData();
                    mes.put("SENDER_ID", promise.getString("USER_ID"));    // 发送人员ID
                    mes.put("SENDER_NAME", promise.getString("USER_ID"));    // 发送人员姓名
                    mes.put("SYNOPSIS", "友情提示：有一条承诺书需要查看");    // 站内信标题
                    mes.put("RECEIVER_ID", cover.get(0).getString("USER_ID"));    // 接收人员ID
                    mes.put("CONTENT", "有一条承诺书数据在“我的-安全承诺-接收承诺列表”，等您查看");// 站内信内容
                }
            }
        }
        map.put("result", errInfo);
        return map;
    }

    /**
     * 手机承诺书列表
     *
     * @param
     * @throws Exception
     */
    @RequestMapping(value = "/promiselist_II")
    @ResponseBody
    public Object promiselist(Page page) throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();
        String errInfo = "success";
        PageData pd = new PageData();
        pd = this.getPageData();
        page.setPd(pd);
        List<PageData> varList = corpPromisePeopleService.promiselist(page);
        map.put("varList", varList);
        map.put("page", page);
        map.put("result", errInfo);
        return map;
    }

    /**
     * 手机接收承诺书列表
     *
     * @param
     * @throws Exception
     */
    @RequestMapping(value = "/receivePromiseList_II")
    @ResponseBody
    public Object receivePromiseList(Page page) throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();
        String errInfo = "success";
        PageData pd = new PageData();
        pd = this.getPageData();
        page.setPd(pd);
        List<PageData> varList = corpPromisePeopleService.receivePromiselist(page);
        map.put("varList", varList);
        map.put("page", page);
        map.put("result", errInfo);
        return map;
    }

    /**
     * 查看
     *
     * @param
     * @throws Exception
     */
    @RequestMapping(value = "/goEdit_II")
    @ResponseBody
    public Object goEdit() throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();
        String errInfo = "success";
        PageData pd = new PageData();
        pd = this.getPageData();
        PageData promise = corpPromiseService.findById(pd);
        List<PageData> promistDetail = corpPromiseDetailsService.findByPromiseId(pd);
        List<PageData> coverpeople = corpPromisePeopleService.findByCoverPromiseId(pd);  //被承诺人
        pd.put("ISSIGN", 1);
        PageData people = corpPromisePeopleService.findById(pd);//承诺人
        map.put("promistDetail", promistDetail);
        map.put("varList", promise);
        map.put("people", people);
        map.put("coverpeople", coverpeople);
        map.put("result", errInfo);
        return map;
    }

    /**
     * 修改阅读状态查看
     *
     * @param
     * @throws Exception
     */
    @RequestMapping(value = "/editIsRead_II")
    @ResponseBody
    public Object editIsRead() throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();
        String errInfo = "success";
        PageData pd = new PageData();
        pd = this.getPageData();
        pd.put("ISREAD", "1");
        corpPromisePeopleService.editIsRead(pd); //阅读状态修改为已阅
        map.put("result", errInfo);
        return map;
    }
}
