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.PrjCompletecompany;
import com.liang.service.PrjBudgetService;
import com.liang.service.PrjCompletecompanyService;
import io.swagger.annotations.ApiOperation;
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;

/**
 * 完成单位(PrjCompletecompany)表控制层
 *
 * @author makejava
 * @since 2023-03-10 11:21:40
 */
@Controller
@RequestMapping("prjCompletecompany")
public class PrjCompletecompanyController extends BaseController {

    /**
     * 服务对象
     */
    @Resource
    private PrjCompletecompanyService prjCompletecompanyService;
    @Resource
    private PrjBudgetService budgetService;

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

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

    /**
     * 编辑数据
     *
     * @param prjCompletecompany 实体
     * @return 编辑结果
     */
    @PutMapping
    public ResponseEntity<PrjCompletecompany> edit(PrjCompletecompany prjCompletecompany) {
        return ResponseEntity.ok(this.prjCompletecompanyService.update(prjCompletecompany));
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @DeleteMapping
    public ResponseEntity<Boolean> deleteById(Integer id) {
        return ResponseEntity.ok(this.prjCompletecompanyService.deleteById(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<PrjCompletecompany> list = prjCompletecompanyService.getCompanyList(paraMap);

        PageInfo<PrjCompletecompany> pageinfo = new PageInfo<>(list);
        //取出查询结果
        List<PrjCompletecompany> 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<PrjCompletecompany> list = prjCompletecompanyService.getCompanyList(paraMap);

        PageInfo<PrjCompletecompany> 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
     */
    @ApiOperation(value = "页面初始化", notes = "页面初始化")
    @RequestMapping(value = "/forAdd", method = RequestMethod.GET)
    public String forAdd() {

        return "prjcompany/add";
    }

    /**
     * 新建项目人员
     *
     * @param completecompany
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/doAdd", method = RequestMethod.POST)
    public BaseResult doAdd(PrjCompletecompany completecompany) {

        int num = prjCompletecompanyService.insert(completecompany);
        if (num > 0) {
            return BaseResult.success("保存成功！");
        } else if (num == -1) {
            return BaseResult.success("该项目项目完成单位已存在，如需修改，请选中后编辑！");
        } else {
            return BaseResult.success("保存失败！");
        }
    }

    @ApiOperation(value = "修改页面初始化", notes = "修改页面初始化")
    @RequestMapping(value = "/forUpdate", method = RequestMethod.GET)
    public String forUpdate(Model model) {
        return "prjcompany/update";
    }

    /**
     * 查看页面
     *
     * @param model
     * @return
     */
    @RequestMapping(value = "/forView", method = RequestMethod.GET)
    public String forView(Model model) {
        return "prjcompany/view";
    }

    /**
     * 批量保存项目单位
     *
     * @param data
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/batchInsert/{xm}", method = RequestMethod.POST)
    public BaseResult batchInsert(@RequestBody String data, @PathVariable Integer xm) {
        //删除项目完成单位
        prjCompletecompanyService.deleteByXm(xm);

        List<PrjCompletecompany> companyList = JsonTool.parseArray(data, PrjCompletecompany.class);
        budgetService.updateYsByXm(xm, companyList.size());

        if (companyList.size() > 0) {
            int num = prjCompletecompanyService.insertBatch(companyList);
            if (num == companyList.size()) {

                return BaseResult.success("完成单位保存成功！");
            } else {
                return BaseResult.failure("完成单位保存失败！");
            }
        } else {
            return BaseResult.success("没有要保存的信息");
        }
    }
}

