package com.liang.controller;

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.PrjDivisioncompany;
import com.liang.service.PrjDivisioncompanyService;
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;

/**
 * 单位分工：子课题单位(PrjDivisioncompany)表控制层
 *
 * @author makejava
 * @since 2023-04-01 19:05:25
 */
@Controller
@RequestMapping("prjDivisioncompany")
public class PrjDivisioncompanyController extends BaseController {
    /**
     * 服务对象
     */
    @Resource
    private PrjDivisioncompanyService prjDivisioncompanyService;

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

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

    /**
     * 新增数据
     *
     * @param prjDivisioncompany 实体
     * @return 新增结果
     */
    @PostMapping
    public ResponseEntity<PrjDivisioncompany> add(PrjDivisioncompany prjDivisioncompany) {
        return ResponseEntity.ok(this.prjDivisioncompanyService.insert(prjDivisioncompany));
    }

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

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

    /**
     * 单位分工
     *
     * @return
     */
    @ApiOperation(value = "页面初始化", notes = "页面初始化")
    @RequestMapping(value = "/forAdd/{xm}", method = RequestMethod.GET)
    public String forAdd(Model model, @PathVariable Integer xm) {
        model.addAttribute("xm", xm);
        return "prjdivisioncompany/add";
    }

    /**
     * 批量保存子课题分工单位
     *
     * @param data
     * @return
     */
    @RequestMapping(value = "/batchInsert/{zkt}", method = RequestMethod.POST)
    @ResponseBody
    public BaseResult batchInsert(@RequestBody String data, @PathVariable Integer zkt) {
        //删除子课题单位分工
        prjDivisioncompanyService.deleteByZkt(zkt);

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

    /**
     * 查看分工单位
     *
     * @return
     */
    @RequestMapping(value = "/forView", method = RequestMethod.GET)
    public String forView() {
        return "prjdivisioncompany/view";
    }

    /**
     * 分工单位列表初始化
     *
     * @param zkt
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/list/{zkt}", method = RequestMethod.POST)
    public BaseResult list(@PathVariable Integer zkt) {
        List<PrjDivisioncompany> list = prjDivisioncompanyService.getCompanyList(zkt);

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

    /**
     * 编辑分工单位
     *
     * @return
     */
    @RequestMapping(value = "/forUpdate", method = RequestMethod.GET)
    public String forUpdate() {
        return "prjdivisioncompany/update";
    }
}

