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.entity.*;
import com.liang.service.PrjUndertakecompanyService;
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;

/**
 * (PrjUndertakecompany)表控制层
 *
 * @author makejava
 * @since 2023-04-09 12:13:15
 */
@Controller
@RequestMapping("prjUndertakecompany")
public class PrjUndertakecompanyController extends BaseController {
    /**
     * 服务对象
     */
    @Resource
    private PrjUndertakecompanyService prjUndertakecompanyService;

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

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

    /**
     * 出资方案承担单位
     *
     * @param
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/list/{czfa}", method = RequestMethod.POST)
    public BaseResult list(@PathVariable Integer czfa) {
        List<PrjUndertakecompany> list = prjUndertakecompanyService.getUtCompanyList(czfa);

        PageInfo<PrjUndertakecompany> pageinfo = new PageInfo<>(list);
        //取出查询结果
        List<PrjUndertakecompany> 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);
    }

    /**
     * @param xm
     * @param pageNum
     * @param pageSize
     * @param dwmc
     * @return
     */
    @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<PrjUndertakecompany> list = prjUndertakecompanyService.getXmUtCompanyList(paraMap);

        PageInfo<PrjUndertakecompany> 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 ysjecz, @RequestParam Double czjecz) {
        model.addAttribute("xm", xm);
        model.addAttribute("ysjecz", ysjecz);
        model.addAttribute("czjecz", czjecz);
        return "prjundertakecompany/add";
    }

    /**
     * 批量保存子课题分工单位
     *
     * @param data
     * @return
     */
    @RequestMapping(value = "/batchInsert/{czfa}", method = RequestMethod.POST)
    @ResponseBody
    public BaseResult batchInsert(@RequestBody String data, @PathVariable Integer czfa) {
        //删除出资方案承担单位
        prjUndertakecompanyService.deleteByCzfa(czfa);

        List<PrjUndertakecompany> companyList = JsonTool.parseArray(data, PrjUndertakecompany.class);
        if (companyList.size() > 0) {
            int num = prjUndertakecompanyService.insertBatch(companyList);
            if (num == companyList.size()) {
                return BaseResult.success("分工单位保存成功！");
            } else {
                return BaseResult.failure("分工单位保存失败！");
            }
        } else {
            return BaseResult.failure("没有要保存的信息！");
        }
    }

    /**
     * 编辑数据
     *
     * @return 编辑结果
     */
    @RequestMapping(value = "/forUpdate", method = RequestMethod.GET)
    public String forUpdate() {
        return "prjundertakecompany/update";
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @DeleteMapping
    public ResponseEntity<Boolean> deleteById(Integer id) {
        return ResponseEntity.ok(this.prjUndertakecompanyService.deleteById(id));
    }

    /**
     * 浏览数据
     *
     * @return
     */
    @RequestMapping(value = "/forView", method = RequestMethod.GET)
    public String forView() {
        return "prjundertakecompany/view";
    }
}

