package com.liang.controller;

import com.liang.common.JsonTool;
import com.liang.common.base.BaseResult;
import com.liang.common.enums.PostInfoEnum;
import com.liang.entity.*;
import com.liang.service.*;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import com.liang.common.base.BaseController;

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

@Controller
@RequestMapping("/prjBudgetRecord")
public class PrjBudgetRecordController extends BaseController{
    @Resource
    private PrjBudgetRecordService prjBudgetRecordService;
    @Resource
    private PrjBudgetService prjBudgetService;
    @Resource
    private PrjApproverecordService prjApproverecordService;
    @Resource
    private PrjCompletecompanyService prjCompletecompanyService;
    @Resource
    private PrjProjectService prjProjectService;

    @ResponseBody
    @RequestMapping(value = "/addUpdateApplyRecord1", method = RequestMethod.POST)
    @Transactional

    public BaseResult addUpdateApplyRecord(@RequestBody String data) {
        List<PrjBudgetRecord> record =new ArrayList<>();
        List<PrjBudget> prjBudgetList = JsonTool.parseArray(data, PrjBudget.class);
        for (int i = 0; i <prjBudgetList.size() ; i++) {
            PrjBudgetRecord prjBudgetRecord = new PrjBudgetRecord();
            prjBudgetRecord.setXmzys(prjBudgetList.get(i).getXmzys());
            prjBudgetRecord.setMc(prjBudgetList.get(i).getMc());
            prjBudgetRecord.setYsje(prjBudgetList.get(i).getYsje());
            prjBudgetRecord.setXm(prjBudgetList.get(i).getXm());
            prjBudgetRecord.setSjje(prjBudgetList.get(i).getSjje());
            prjBudgetRecord.setFj(prjBudgetList.get(i).getFj());
            prjBudgetRecord.setNf(prjBudgetList.get(i).getNf());
            prjBudgetRecord.setPx(prjBudgetList.get(i).getPx());
            prjBudgetRecord.setBm(prjBudgetList.get(i).getBm());
            prjBudgetRecord.setHjbm(prjBudgetList.get(i).getHjbm());
            prjBudgetRecord.setDw1(prjBudgetList.get(i).getDw1());
            prjBudgetRecord.setDw2(prjBudgetList.get(i).getDw2());
            prjBudgetRecord.setDw3(prjBudgetList.get(i).getDw3());
            prjBudgetRecord.setDw4(prjBudgetList.get(i).getDw4());
            prjBudgetRecord.setDw5(prjBudgetList.get(i).getDw5());
            prjBudgetRecord.setDw6(prjBudgetList.get(i).getDw6());
            prjBudgetRecord.setDw7(prjBudgetList.get(i).getDw7());
            prjBudgetRecord.setDw8(prjBudgetList.get(i).getDw8());
            prjBudgetRecord.setDw9(prjBudgetList.get(i).getDw9());
            prjBudgetRecord.setDw10(prjBudgetList.get(i).getDw10());
            prjBudgetRecord.setSjys1(prjBudgetList.get(i).getSjys1());
            prjBudgetRecord.setSjys2(prjBudgetList.get(i).getSjys2());
            prjBudgetRecord.setSjys3(prjBudgetList.get(i).getSjys3());
            prjBudgetRecord.setSjys4(prjBudgetList.get(i).getSjys4());
            prjBudgetRecord.setSjys5(prjBudgetList.get(i).getSjys5());
            prjBudgetRecord.setSjys6(prjBudgetList.get(i).getSjys6());
            prjBudgetRecord.setSjys7(prjBudgetList.get(i).getSjys7());
            prjBudgetRecord.setSjys8(prjBudgetList.get(i).getSjys8());
            prjBudgetRecord.setSjys9(prjBudgetList.get(i).getSjys9());
            prjBudgetRecord.setSjys10(prjBudgetList.get(i).getSjys10());
            prjBudgetRecord.setBz(prjBudgetList.get(i).getBz());
            record.add(prjBudgetRecord);
        }
//先删除表中的预留数据（只能保存一份）
        prjBudgetRecordService.deleteHixstoryRecord(prjBudgetList.get(0).getXm());
//插入到修改申请表中(临时保存表)a
        int i = prjBudgetRecordService.addHistoryRecord(record);
        if (i>0){
            return BaseResult.success();
        }else {
            return BaseResult.failure("");
        }
    }

    /**
     * 查看数据
     *
     * @return
     */
    @RequestMapping(value = "/forView/{id}", method = RequestMethod.GET)
    public String forView(Model model, @PathVariable Integer id) {
        List<PrjBudget> prjBudgetList = prjBudgetService.getOldBudgetList(id);
        //获取审批通过状态
        for (int i = 0; i < prjBudgetList.size(); i++) {
            String sptgzt = prjBudgetList.get(i).getSptgzt();
            String[] strings = sptgzt.split(",");

            for (int j = 0; j < strings.length; j++) {
                switch (j){
                    case 0:
                        if (Double.valueOf(strings[0])!=null){
                            prjBudgetList.get(i).setYsje(Double.valueOf(strings[0]));
                        }else{
                            prjBudgetList.get(i).setYsje(0.0);
                        }
                    case 1:
                        if (Double.valueOf(strings[1])!=null) {
                            prjBudgetList.get(i).setDw1(Double.valueOf(strings[1]));
                        }else {
                            prjBudgetList.get(i).setDw1(0.0);
                        }
                        break;
                    case 2:
                        if (Double.valueOf(strings[2])!=null) {
                            prjBudgetList.get(i).setDw2(Double.valueOf(strings[2]));
                        }else {
                            prjBudgetList.get(i).setDw2(0.0);
                        }
                        break;
                    case 3:
                        if (Double.valueOf(strings[3])!=null) {
                            prjBudgetList.get(i).setDw3(Double.valueOf(strings[3]));
                        }else {
                            prjBudgetList.get(i).setDw3(0.0);
                        }
                        break;
                    case 4:
                        if (Double.valueOf(strings[4])!=null) {
                            prjBudgetList.get(i).setDw4(Double.valueOf(strings[4])); }else {
                            prjBudgetList.get(i).setDw4(0.0);
                        }    break;
                    case 5:
                        if (Double.valueOf(strings[5])!=null) {
                            prjBudgetList.get(i).setDw5(Double.valueOf(strings[5])); }else {
                            prjBudgetList.get(i).setDw5(0.0);
                        }    break;
                    case 6:
                        if (Double.valueOf(strings[6])!=null) {
                            prjBudgetList.get(i).setDw6(Double.valueOf(strings[6]));  }else {
                            prjBudgetList.get(i).setDw6(0.0);
                        }   break;
                    case 7:
                        if (Double.valueOf(strings[7])!=null) {
                            prjBudgetList.get(i).setDw7(Double.valueOf(strings[7])); }else {
                            prjBudgetList.get(i).setDw7(0.0);
                        }    break;
                    case 8:
                        if (Double.valueOf(strings[8])!=null) {
                            prjBudgetList.get(i).setDw8(Double.valueOf(strings[8]));  }else {
                            prjBudgetList.get(i).setDw8(0.0);
                        }   break;
                    case 9:
                        if (Double.valueOf(strings[9])!=null) {
                            prjBudgetList.get(i).setDw9(Double.valueOf(strings[9]));  }else {
                            prjBudgetList.get(i).setDw9(0.0);
                        }   break;
                    case 10:
                        if (Double.valueOf(strings[10])!=null) {
                            prjBudgetList.get(i).setDw10(Double.valueOf(strings[10]));   }else {
                            prjBudgetList.get(i).setDw10(0.0);
                        }  break;

                }
            }
        }
        List<PrjBudgetRecord> record = prjBudgetRecordService.queryRecordByXM(id);
        model.addAttribute("prjBudget",prjBudgetList);
        model.addAttribute("prjBudgetRecord",record);
        return "prjproject/xgview_record";
    }

    /**
     * 待审批列表
     *
     * @return //
     */
    @RequestMapping(value = "/forApproveUpdate/{xmid}/{ysje}", method = RequestMethod.GET)
    public String forApproveUpdate(Model model, @PathVariable Integer xmid, @PathVariable Double
            ysje, @RequestParam String spzt) {
        //系统当前用户
        SysUserInfo userInfo = getSysUserInfo();
        model.addAttribute("userInfo", userInfo);

        Map<String, String> paraMap1 = new HashMap<>();
        paraMap1.put("xmid", xmid.toString());
        paraMap1.put("zt", spzt);
        paraMap1.put("wc", "0");
        List<PrjApproverecord> approveList = prjApproverecordService.getApproveByXmId(paraMap1);
        if (approveList.size() > 0){
            model.addAttribute("approveInfo", approveList.get(approveList.size() - 1));
        }

            return "prjproject/finish_xg_approve";

    }

    /**
     * 将中间表中的数据同步到任务书明细表中
     * */
    @ResponseBody
    @RequestMapping(value = "/doUpdate/{xmid}", method = RequestMethod.POST)
    @Transactional
    public BaseResult bathUpdate( @PathVariable Integer xmid) {
//  1.获取年度预算中间表情数据
//        List<PrjAnnualBudgetRecord> record = prjAnnualBudgetRecordService.queryRecordByNdysid(xmid);
        List<PrjBudgetRecord> prjBudgetRecordList = prjBudgetRecordService.queryRecordByXM(xmid);
//  2.修改年度预算明细表的内容
        int i =prjBudgetService.updateXg(prjBudgetRecordList);
//        int  i =prjAnnualbudgetdetailService.updateXg(record);
        if (i>0){
// 3.保存成功后删除中间表中的数据
            prjBudgetRecordService.deleteHixstoryRecord(xmid);
//  4.修改年度预算表中的sfsp状态
            prjProjectService.updateSFXG(xmid);
            return BaseResult.success();
        }else {
            return BaseResult.failure("记录保存失败");
        }


    }

    /**
     * 审批列表
     *
     * @return
     */
    @RequestMapping(value = "/forApproveList", method = RequestMethod.GET)
    public String forApproveList() {
        return "prjproject/xglist_approve";
    }
}
