package com.liang.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.liang.common.JsonTool;
import com.liang.common.base.BaseController;
import com.liang.common.base.BaseResult;
import com.liang.common.enums.ProjectTypeEnnum;
import com.liang.entity.PrjAnnualbudget;
import com.liang.entity.PrjFeescheme;
import com.liang.entity.PrjProject;
import com.liang.service.PrjFeeschemeService;
import com.liang.service.PrjProjectService;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

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

/**
 * 出资方案(PrjFeescheme)表控制层
 *
 * @author makejava
 * @since 2023-04-09 13:54:36
 */
@Controller
@RequestMapping("prjFeescheme")
public class PrjFeeschemeController extends BaseController {
    /**
     * 服务对象
     */
    @Resource
    private PrjFeeschemeService prjFeeschemeService;
    @Resource
    private PrjProjectService prjProjectService;

    /**
     * 分页查询
     *
     * @param prjFeescheme 筛选条件
     * @param pageRequest  分页对象
     * @return 查询结果
     */
    @GetMapping
    public ResponseEntity<Page<PrjFeescheme>> queryByPage(PrjFeescheme prjFeescheme, PageRequest pageRequest) {
        return ResponseEntity.ok(this.prjFeeschemeService.queryByPage(prjFeescheme, pageRequest));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public ResponseEntity<PrjFeescheme> queryById(@PathVariable("id") Integer id) {
        return ResponseEntity.ok(this.prjFeeschemeService.queryById(id));
    }


    /**
     * 获取出资方案列表
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/list/{xm}", method = RequestMethod.POST)
    public BaseResult list(@PathVariable Integer xm) {
        Map<String, String> paraMap = new HashMap<>();
        paraMap.put("xm", xm.toString());
        List<PrjFeescheme> list = prjFeeschemeService.getFeeSchemeList(paraMap);

        PageInfo<PrjFeescheme> pageinfo = new PageInfo<>(list);
        //取出查询结果
        List<PrjFeescheme> rows = pageinfo.getList();
        int total = (int) pageinfo.getTotal();
        Map<String, Object> result = new HashMap<>();
        result.put(RESULT_ROWS, rows);
        result.put(RESULT_TOTAL, total);

        return BaseResult.success(result);
    }

    @ResponseBody
    @RequestMapping(value = "/dwlist/{xm}", method = RequestMethod.GET)
    public Object dwlist(@PathVariable Integer xm,
                         @RequestParam("page") Integer pageNum,
                         @RequestParam("limit") Integer pageSize,
                         @RequestParam(required = false, defaultValue = "", value = "dwmc") String dwmc) {
        PageHelper.startPage(pageNum, pageSize);

        Map<String, String> paraMap = new HashMap<>();
        paraMap.put("xm", xm.toString());
        paraMap.put("dwmc", dwmc);
        List<PrjFeescheme> list = prjFeeschemeService.getFeeSchemeList(paraMap);

        PageInfo<PrjFeescheme> pageinfo = new PageInfo<>(list);
        // 取出查询结果
        int total = (int) pageinfo.getTotal();

        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("code", 0);
        resultMap.put("msg", "");
        resultMap.put("count", total);
        resultMap.put("data", list);

        return resultMap;
    }

    /**
     * 新增数据
     *
     * @return 新增结果
     */
    @RequestMapping(value = "/forAdd/{xm}", method = RequestMethod.GET)
    public String forAdd(Model model, @PathVariable Integer xm, @RequestParam String qs, @RequestParam String wc, @RequestParam String ysjecz) {
        model.addAttribute("xm", xm);
        model.addAttribute("qs", qs.substring(0, 4));
        model.addAttribute("wc", wc.substring(0, 4));
        model.addAttribute("ysjecz", ysjecz);
        return "prjfeescheme/add";
    }

    /**
     * 保存数据
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/doAdd", method = RequestMethod.POST)
    public BaseResult doAdd(PrjFeescheme feescheme) {
        int num = prjFeeschemeService.insert(feescheme);
        if (num > 0) {
            return BaseResult.success("出资方案保存成功!", feescheme);
        } else if (num == -1) {
            return BaseResult.failure("该出资方案已存在!");
        } else {
            return BaseResult.failure("出资方案保存失败!");
        }
    }

    /**
     * 编辑数据
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/doUpdate", method = RequestMethod.POST)
    public BaseResult doUpdate(PrjFeescheme feescheme) {
        int num = prjFeeschemeService.update(feescheme);
        if (num > 0) {
            return BaseResult.success("出资方案保存成功!", feescheme);
        } else if (num == -1) {
            return BaseResult.failure("该出资方案已存在!");
        } else {
            return BaseResult.failure("出资方案保存失败!");
        }
    }

    @RequestMapping(value = "/forUpdate/{id}", method = RequestMethod.GET)
    public String forUpdate(Model model, @PathVariable Integer id, @RequestParam String qs, @RequestParam String wc, @RequestParam String ysjecz) {
        PrjFeescheme feescheme = prjFeeschemeService.queryById(id);
        model.addAttribute("feescheme", feescheme);
        model.addAttribute("qs", qs.substring(0, 4));
        model.addAttribute("wc", wc.substring(0, 4));
        model.addAttribute("ysjecz", ysjecz);
        return "prjfeescheme/update";
    }

    @RequestMapping(value = "/forView/{id}", method = RequestMethod.GET)
    public String forView(Model model, @PathVariable Integer id) {
        PrjFeescheme feescheme = prjFeeschemeService.queryById(id);
        model.addAttribute("feescheme", feescheme);

        return "prjfeescheme/view";
    }

    /**
     * 删除数据
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/delete/{id}", method = RequestMethod.GET)
    @ResponseBody
    public BaseResult delete(@PathVariable Integer id) {
        boolean flag = prjFeeschemeService.deleteById(id);
        if (flag) {
            return BaseResult.success("删除成功!");
        } else {
            return BaseResult.failure("删除失败!");
        }
    }

    /**
     * 批量删除数据
     *
     * @param data
     * @return
     */
    @RequestMapping(value = "/batchDelete", method = RequestMethod.POST)
    @ResponseBody
    public BaseResult batchDelete(@RequestBody String data) {

        List<Integer> idList = JsonTool.getListID(data);
        int num = prjFeeschemeService.batchDelete(idList);
        if (num > 0) {
            return BaseResult.success("批量删除成功!");
        } else {
            return BaseResult.failure("批量删除失败!");
        }
    }

    /**
     * 获取年度预算列表
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/ndczje/{rws}", method = RequestMethod.POST)
    public double ndczje(@PathVariable Integer rws, @RequestParam String nf) {
        Map<String, String> paraMap = new HashMap<>();
        paraMap.put("xm", rws.toString());
        paraMap.put("nf", nf);
        double ndczje;
        PrjProject prjProject = prjProjectService.queryById(rws);
        if (ProjectTypeEnnum.GWGSKJXM.msg().equals(prjProject.getXmlxmc())) {
            ndczje = prjFeeschemeService.getNdFeeSchemeofDKY(paraMap);
        } else {
            ndczje = prjFeeschemeService.getNdFeeScheme(paraMap);
        }


        return ndczje;
    }
}

