package com.liang.controller;

import com.liang.common.JsonTool;
import com.liang.common.base.BaseController;
import com.liang.common.base.BaseResult;
import com.liang.entity.PrjAnnualBudgetRecord;
import com.liang.entity.PrjApproverecord;
import com.liang.entity.PrjContract;
import com.liang.entity.PrjContractXg;
import com.liang.service.PrjApproverecordService;
import com.liang.service.PrjContractXgService;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * (PrjContractXg)表控制层
 */
@Controller
@RequestMapping("prjContractXg")
public class PrjContractXgController extends BaseController {
    @Resource
    PrjContractXgService prjContractXgService;

    /**
     * 保存数据
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/doAdd", method = RequestMethod.POST)
    @Transactional
    public BaseResult doAdd(PrjContract prjContract) {
        prjContract.setSpzt("未提交");
    //  1.清空修改中间表中存在的数据
        prjContractXgService.deldteById(prjContract.getId());
    //  2.插入修改后的数据
        int num = prjContractXgService.insert(prjContract);
        if (num > 0) {
            return BaseResult.success("保存成功!", prjContract);
        } else {
            return BaseResult.failure("保存失败!");
        }
    }
    /**
     * 更新审批状态
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/updateSpzt", method = RequestMethod.POST)
    public BaseResult updateSpzt(@RequestBody String strData) {
        PrjContract prjContract = JsonTool.parseObject(strData, PrjContract.class);
        int num = prjContractXgService.updateSpzt(prjContract);
        if (prjContract.getSpzt().equals("审批结束")){
            prjContractXgService.doUpdate(prjContract.getId());
        }
        if (num > 0) {
            return BaseResult.success("保存成功!");
        } else {
            return BaseResult.failure("保存失败!");
        }
    }
    @RequestMapping(value = "/ViewXGSQ", method = RequestMethod.GET)
    public String ViewXGSQ(Model model) {
//            获取待审批修改申请列表
//            prjAnnualBudgetRecordService.getSpList();

        return "prjcontract/view_xgsq";
    }

    /**
     * 浏览数据
     *
     * @return
     */
    @RequestMapping(value = "/forView/{id}", method = RequestMethod.GET)
    public String forView(Model model, @PathVariable Integer id) {
        //合同
        PrjContractXg contract = prjContractXgService.queryByhtId(id);
        model.addAttribute("contract", contract);

        return "prjcontract/view_xg";
    }
    /**
     * 将中间表中的数据同步到合同表中
     * */
    @ResponseBody
    @RequestMapping(value = "/doUpdate/{xmid}", method = RequestMethod.POST)
    @Transactional
    public BaseResult bathUpdate( @PathVariable Integer xmid) {

        this.prjContractXgService.doUpdate(xmid);
        return BaseResult.success();


    }

    @Resource
    private  PrjApproverecordService prjApproverecordService;
    @ResponseBody
    @RequestMapping(value = "/updateHtht",method = RequestMethod.POST)
    public BaseResult updateSpzt(@RequestBody PrjContractXg prjContractXg){
        // 获取回退的数据
        // 获取当前系统时间
        Date data = new Date();
        // 转换时间格式
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        prjContractXg.setSpzt("未锁定");
        int mun = prjContractXgService.updateHtht(prjContractXg);
        if (mun == 0){
            return BaseResult.failure(1,"合同数据回退失败！");
        }
        PrjApproverecord prjApproverecord = new PrjApproverecord();
        prjApproverecord.setTablename("prj_Contract");
        prjApproverecord.setWc(true);
        prjApproverecord.setTjsj(format.format(data));
        prjApproverecord.setShsj(format.format(data));
        prjApproverecord.setXmid(prjContractXg.getId());
        prjApproverecord.setTjr(getSysUserId());
        prjApproverecord.setShr(getSysUserId());
        prjApproverecord.setSpcz("合同数据解锁");
        prjApproverecord.setSffh(true);
        prjApproverecord.setZt("合同解锁");
        int num = prjApproverecordService.insert(prjApproverecord);
        if (num == 0){
            return BaseResult.failure(1,"合同数据回退失败");
        }
        return BaseResult.success("合同数据回退成功！");
    }
}
