package com.zcloud.controller.commitment;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
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.util.DateUtil;
import com.zcloud.util.Jurisdiction;
import com.zcloud.util.Tools;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

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("/corppromise")
public class CorpPromisePart_IController extends BaseController {

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

    /**新增
     * @param
     * @throws Exception
     */
    @RequestMapping(value="/add")
    @ResponseBody
    public Object add() throws Exception{
        Map<String,Object> map = new HashMap<String,Object>();
        String errInfo = "success";
        PageData pd = new PageData();
        pd = this.getPageData();
        pd.put("PROMISE_ID", this.get32UUID());	//主键
        pd.put("ISDELETE",0);
        pd.put("STATE","0");
        pd.put("CORPINFO_ID",Jurisdiction.getCORPINFO_ID());
        List<JSONObject> people = (List<JSONObject>) JSON.parse(pd.getString("people"));
        pd.put("PEOPLENUM_INVOLVED",String.valueOf(people.size()));//获取人数
        pd.put("CREATOR", Jurisdiction.getUSER_ID());	//创建人
        pd.put("CREATTIME", DateUtil.date2Str(new Date()));	//创建时间
        pd.put("OPERATOR", Jurisdiction.getUSER_ID());	//创建人
        pd.put("OPERATTIME", DateUtil.date2Str(new Date()));	//创建时间
        pd.put("RELEASE_PLATFORM",pd.getString("DISTRIBUTIONOBJECT"));//企业端发布
        corpPromiseService.save(pd);

        List<JSONObject> list = (List<JSONObject>) JSON.parse(pd.getString("DETAIL")); //副文内容
        if(list.size()>0){
            for (int i=0;i<list.size();i++) {
                PageData promise = new PageData();
                promise.put("PROMISEDETAIL_ID", this.get32UUID());    //主键
                promise.put("PROMISE_ID", pd.getString("PROMISE_ID"));    //外键ID
                promise.put("COLLATERAL",list.get(i).get("value"));
                promise.put("SORTINDEX",list.get(i).get("index"));
                corpPromiseDetailsService.save(promise);
            }
        }
        if(people.size()>0){  //承诺人数据
            int num = 0;
            for (int i=0;i<people.size();i++) {
                PageData promise = new PageData();
                promise.put("PROMISE_ID", pd.getString("PROMISE_ID"));    //外键ID
                promise.put("DEPARTMENT_ID",people.get(i).get("DEPARTMENT_ID"));
                promise.put("POST_ID",people.get(i).get("POST_ID"));
                List<JSONObject> user = (List<JSONObject>) people.get(i).get("USER_ID"); //同部门、同岗位下有可能选择多个人
                promise.put("ISSIGN",0);
                if(user.size()>0){
                    for(int j=0;j<user.size();j++){
                        promise.put("USER_ID",user.get(j));
                        promise.put("PROMISEPEOPLE_ID", this.get32UUID());    //主键
                        promise.put("CORPINFO_ID",people.get(i).get("CORPINFO_ID"));
                        promise.put("ISPROMISE_PEOPLE","0");
                        corpPromisePeopleService.save(promise);
                        num++;
                    }
                }
            }
            pd.put("PEOPLENUM_INVOLVED",num);
            corpPromiseService.editpeople(pd);
        }
        List<JSONObject> coverpeople = (List<JSONObject>) JSON.parse(pd.getString("coverpeople"));
        if(coverpeople.size()>0){  //被承诺人数据
            for (int i=0;i<coverpeople.size();i++) {
                PageData promise = new PageData();
                promise.put("PROMISE_ID", pd.getString("PROMISE_ID"));    //外键ID
                promise.put("DEPARTMENT_ID",coverpeople.get(i).get("DEPARTMENT_ID"));
                promise.put("POST_ID",coverpeople.get(i).get("POST_ID"));
                List<JSONObject> user = (List<JSONObject>) coverpeople.get(i).get("USER_ID"); //同部门、同岗位下有可能选择多个人
                promise.put("ISSIGN",0);
                if(user.size()>0){
                    for(int j=0;j<user.size();j++){
                        promise.put("USER_ID",user.get(j));
                        promise.put("PROMISEPEOPLE_ID", this.get32UUID());    //主键
                        promise.put("CORPINFO_ID",coverpeople.get(i).get("CORPINFO_ID"));
                        promise.put("ISPROMISE_PEOPLE","1");
                        corpPromisePeopleService.save(promise);
                    }
                }
            }
        }

        map.put("result", errInfo);
        return map;
    }

    /**列表
     * @param page
     * @throws Exception
     */
    @RequestMapping(value="/list")
    @ResponseBody
    public Object list(Page page) throws Exception{
        Map<String,Object> map = new HashMap<String,Object>();
        String errInfo = "success";
        PageData pd = new PageData();
        pd = this.getPageData();
        pd.put("CORPINFO_ID",Jurisdiction.getCORPINFO_ID());
        String KEYWORDS = pd.getString("KEYWORDS");						//关键词检索条件
        if(Tools.notEmpty(KEYWORDS))pd.put("KEYWORDS", KEYWORDS.trim());
        page.setPd(pd);
        List<PageData> varList = corpPromiseService.list(page);	//列出Notice列表
        map.put("varList", varList);
        map.put("page", page);
        map.put("result", errInfo);
        return map;
    }

    /**启用禁用
     * @param
     * @throws Exception
     */
    @RequestMapping(value="/editdelete")
    @ResponseBody
    public Object editdelete() throws Exception{
        Map<String,Object> map = new HashMap<String,Object>();
        String errInfo = "success";
        PageData pd = new PageData();
        pd = this.getPageData();
        corpPromiseService.editdelete(pd);
        map.put("result", errInfo);
        return map;
    }

    /**查看
     * @param
     * @throws Exception
     */
    @RequestMapping(value="/goEdit")
    @ResponseBody
    public Object goEdit() throws Exception{
        Map<String,Object> map = new HashMap<String,Object>();
        String errInfo = "success";
        PageData pd = new PageData();
        pd = this.getPageData();
        pd.put("CORPINFO_ID",Jurisdiction.getCORPINFO_ID());
        PageData promise =  corpPromiseService.findById(pd);
        List<PageData> promistDetail = corpPromiseDetailsService.findByPromiseId(pd);
        List<PageData> coverpeople = corpPromisePeopleService.findByCoverPromiseId(pd);  //被承诺人
        List<PageData> people = corpPromisePeopleService.findByPromiseId(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="/edit")
    @ResponseBody
    public Object edit() throws Exception{
        Map<String,Object> map = new HashMap<String,Object>();
        String errInfo = "success";
        PageData pd = new PageData();
        pd = this.getPageData();
        pd.put("OPERATTIME", DateUtil.date2Str(new Date()));	//创建时间
        corpPromiseService.edit(pd);
        corpPromiseDetailsService.deletePromiseId(pd);
        List<JSONObject> list = (List<JSONObject>) JSON.parse(pd.getString("DETAIL"));
        if(list.size()>0){
            for (int i=0;i<list.size();i++) {
                PageData promise = new PageData();
                promise.put("PROMISEDETAIL_ID", this.get32UUID());    //主键
                promise.put("PROMISE_ID", pd.getString("PROMISE_ID"));    //外键ID
                promise.put("COLLATERAL",list.get(i).get("value"));
                promise.put("SORTINDEX",list.get(i).get("index"));
                corpPromiseDetailsService.save(promise);
            }
        }
        corpPromisePeopleService.deletePromiseId(pd);  //先删除承诺人、被承诺人数据，再新增
        List<JSONObject> people = (List<JSONObject>) JSON.parse(pd.getString("people"));
        if(people.size()>0){  //承诺人数据
            int num = 0;
            for (int i=0;i<people.size();i++) {
                PageData promise = new PageData();
                promise.put("PROMISE_ID", pd.getString("PROMISE_ID"));    //外键ID
                promise.put("DEPARTMENT_ID",people.get(i).get("DEPARTMENT_ID"));
                promise.put("POST_ID",people.get(i).get("POST_ID"));
                List<JSONObject> user = (List<JSONObject>) people.get(i).get("USER_ID"); //同部门、同岗位下有可能选择多个人
                promise.put("ISSIGN",0);
                if(user.size()>0){
                    for(int j=0;j<user.size();j++){
                        promise.put("USER_ID",user.get(j));
                        promise.put("PROMISEPEOPLE_ID", this.get32UUID());    //主键
                        promise.put("CORPINFO_ID",people.get(i).get("CORPINFO_ID"));
                        promise.put("ISPROMISE_PEOPLE","0");
                        corpPromisePeopleService.save(promise);
                        num++;
                    }
                }
            }
            pd.put("PEOPLENUM_INVOLVED",num);
            corpPromiseService.editpeople(pd);
        }

        List<JSONObject> coverpeople = (List<JSONObject>) JSON.parse(pd.getString("coverpeople"));
        if(coverpeople.size()>0){  //被承诺人数据
            for (int i=0;i<coverpeople.size();i++) {
                PageData promise = new PageData();
                promise.put("PROMISE_ID", pd.getString("PROMISE_ID"));    //外键ID
                promise.put("DEPARTMENT_ID",coverpeople.get(i).get("DEPARTMENT_ID"));
                promise.put("POST_ID",coverpeople.get(i).get("POST_ID"));
                List<JSONObject> user = (List<JSONObject>) coverpeople.get(i).get("USER_ID"); //同部门、同岗位下有可能选择多个人
                promise.put("ISSIGN",0);
                if(user.size()>0){
                    for(int j=0;j<user.size();j++){
                        promise.put("USER_ID",user.get(j));
                        promise.put("PROMISEPEOPLE_ID", this.get32UUID());    //主键
                        promise.put("CORPINFO_ID",coverpeople.get(i).get("CORPINFO_ID"));
                        promise.put("ISPROMISE_PEOPLE","1");
                        corpPromisePeopleService.save(promise);
                    }
                }
            }
        }
        map.put("result", errInfo);
        return map;
    }

    /**查看正文
     * @param
     * @throws Exception
     */
    @RequestMapping(value="/showtemplatetext")
    @ResponseBody
    public Object showtemplate(Page page) throws Exception{
        Map<String,Object> map = new HashMap<String,Object>();
        String errInfo = "success";
        PageData pd = new PageData();
        pd = this.getPageData();
        String KEYWORDS = pd.getString("KEYWORDS");						//关键词检索条件
        if(Tools.notEmpty(KEYWORDS))pd.put("KEYWORDS", KEYWORDS.trim());
        if (!"admin".equals(Jurisdiction.getUsername())){
            pd.put("CORPINFO_ID",Jurisdiction.getCORPINFO_ID());
        }
        page.setPd(pd);
        List<PageData> varList = corpPromiseService.listAll(page);	//列出Notice列表
        map.put("varList", varList);
        map.put("page", page);
        map.put("result", errInfo);
        return map;
    }

    /**查看副文
     * @param
     * @throws Exception
     */
    @RequestMapping(value="/collateral")
    @ResponseBody
    public Object collateral() throws Exception{
        Map<String,Object> map = new HashMap<String,Object>();
        String errInfo = "success";
        PageData pd = new PageData();
        pd = this.getPageData();
        List<PageData> varList = corpPromiseService.collateralall(pd);	//列出Notice列表
        map.put("varList", varList);
        map.put("result", errInfo);
        return map;
    }

    /**查看承诺人签署情况
     * @param
     * @throws Exception
     */
    @RequestMapping(value="/peopledetails")
    @ResponseBody
    public Object peopledetails(Page page) throws Exception{
        Map<String,Object> map = new HashMap<String,Object>();
        String errInfo = "success";
        PageData pd = new PageData();
        pd = this.getPageData();
        pd.put("ISPROMISE_PEOPLE","0");
        page.setPd(pd);
        List<PageData> varList = corpPromisePeopleService.list(page);	//列出Notice列表
        map.put("varList", varList);
        map.put("page", page);
        map.put("result", errInfo);
        return map;
    }

    /**查看承诺人签署情况
     * @param
     * @throws Exception
     */
    @RequestMapping(value="/peopleview")
    @ResponseBody
    public Object peopleview() throws Exception{
        Map<String,Object> map = new HashMap<String,Object>();
        String errInfo = "success";
        PageData pd = new PageData();
        pd = this.getPageData();
        PageData TEXT = corpPromiseService.findById(pd);	//正文
        List<PageData> COLLATERAL = corpPromiseDetailsService.findByPromiseId(pd);	//副文
        PageData ISGN = corpPromisePeopleService.findById(pd);
        List<PageData> coverpeople = corpPromisePeopleService.findByCoverPromiseId(pd);  //被承诺人
        map.put("varList", TEXT);
        map.put("COLLATERAL",COLLATERAL);
        map.put("COVERPEOPLE",coverpeople);
        map.put("ISGN",ISGN);
        map.put("result", errInfo);
        return map;
    }


    /**查询岗位信息列表
     * @param
     * @throws Exception
     */
    @RequestMapping(value="/postlistAll")
    @ResponseBody
    public Object listAll() throws Exception{
        Map<String,Object> map = new HashMap<String,Object>();
        String errInfo = "success";
        PageData pd = new PageData();
        pd = this.getPageData();
        pd.put("STATUS", "0");
        List<PageData>	varList = corpPromiseService.postlistAll(pd);	//列出Post列表
        map.put("postList", varList);
        map.put("result", errInfo);
        return map;
    }

    /**根据部门岗位查询用户列表
     * @param
     * @throws Exception
     */
    @RequestMapping(value="/userlistAll")
    @ResponseBody
    public Object userlistAll() throws Exception{
        Map<String,Object> map = new HashMap<String,Object>();
        String errInfo = "success";
        PageData pd = new PageData();
        pd = this.getPageData();
        List<PageData>	userList = corpPromiseService.listAllUser(pd);	//列出Post列表
        map.put("userList", userList);
        map.put("result", errInfo);
        return map;
    }

    /**企业端统计功能
     * @param
     * @throws Exception
     */
    @RequestMapping(value="/corppromise")
    @ResponseBody
    public Object corppromise(Page page) throws Exception{
        Map<String,Object> map = new HashMap<String,Object>();
        String errInfo = "success";
        PageData pd = new PageData();
        pd = this.getPageData();
        pd.put("ISPROMISE_PEOPLE","0");
        page.setPd(pd);
        List<PageData> varList = corpPromisePeopleService.corppromiselist(page);	//各企业安全承诺列表
        map.put("varList", varList);
        map.put("page",page);
        map.put("result", errInfo);
        return map;
    }

    /**统计企业端承诺书查看功能
     * @param
     * @throws Exception
     */
    @RequestMapping(value="/corppromiseDetail")
    @ResponseBody
    public Object corppromiseDetail(Page page) throws Exception{
        Map<String,Object> map = new HashMap<String,Object>();
        String errInfo = "success";
        PageData pd = new PageData();
        pd = this.getPageData();
        pd.put("ISPROMISE_PEOPLE","0");
        page.setPd(pd);
        List<PageData> varList = corpPromisePeopleService.corppromiseDetaillist(page);	//企业安全承诺列表查看
        map.put("varList", varList);
        map.put("page",page);
        map.put("result", errInfo);
        return map;
    }

    /**承诺书列表点查看
     * @param
     * @throws Exception
     */
    @RequestMapping(value="/peopleDetail")
    @ResponseBody
    public Object peopleDetail(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.peopleDetaillist(page);	//企业安全承诺列表查看
        map.put("varList", varList);
        map.put("page",page);
        map.put("result", errInfo);
        return map;
    }

}
