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.CorpPromisePeopleService;
import com.zcloud.service.commitment.PromiseDetailsService;
import com.zcloud.service.commitment.PromiseService;
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("/promise")
public class PromiseController extends BaseController {

    @Resource
    private PromiseService promiseService;
    @Resource
    private PromiseDetailsService promiseDetailsService;
    @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("CREATOR", "admin");	//创建人
        pd.put("CREATTIME", DateUtil.date2Str(new Date()));	//创建时间
        pd.put("OPERATOR", "admin");	//创建人
        pd.put("OPERATTIME", DateUtil.date2Str(new Date()));	//创建时间
        pd.put("CORPINFO_ID",Jurisdiction.getCORPINFO_ID());
        promiseService.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"));
                promiseDetailsService.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();
        String KEYWORDS = pd.getString("KEYWORDS");						//关键词检索条件
        if(Tools.notEmpty(KEYWORDS))pd.put("KEYWORDS", KEYWORDS.trim());
        pd.put("CORPINFO_ID",Jurisdiction.getCORPINFO_ID());
        page.setPd(pd);
        List<PageData> varList = promiseService.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();
        promiseService.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();
        PageData promise =  promiseService.findById(pd);
        List<PageData> promistDetail = promiseDetailsService.findByPromiseId(pd);
        map.put("promistDetail",promistDetail);
        map.put("varList", promise);
        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()));	//创建时间
        promiseService.edit(pd);
        promiseDetailsService.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"));
                promiseDetailsService.save(promise);
            }
        }
        map.put("result", errInfo);
        return map;
    }
    /**启用禁用
     * @param
     * @throws Exception
     */
    @RequestMapping(value="/delete")
    @ResponseBody
    public Object delete() throws Exception{
        Map<String,Object> map = new HashMap<String,Object>();
        String errInfo = "success";
        PageData pd = new PageData();
        pd = this.getPageData();
        List<PageData> coverpeople = corpPromisePeopleService.findByCoverPromiseId(pd);  //被承诺人
        List<PageData> people = corpPromisePeopleService.findByPromiseId(pd);//承诺人
        if(coverpeople.size()>0){
            map.put("result", "fail");
            map.put("msg", "该承诺书有被承诺人,删除失败!");
            return map;
        }else if(people.size()>0){
            map.put("result", "fail");
            map.put("msg", "该承诺书有承诺人,删除失败!");
            return map;
        }else{
           promiseService.delete(pd); //  删除
        }
        map.put("result", errInfo);
        return map;
    }
}
