package com.liang.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.liang.common.JsonTool;
import com.liang.common.annotation.LogAround;
import com.liang.common.base.BaseController;
import com.liang.common.base.BaseResult;
import com.liang.entity.BaseProjecttemplate;
import com.liang.service.*;
//import com.liang.vo.service.*;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

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

/**
 * 项目模板(BaseProjecttemplate)表控制层
 *
 * @author makejava
 * @since 2023-03-14 15:30:35
 */
@Controller
@RequestMapping("baseProjecttemplate")
public class BaseProjecttemplateController extends BaseController {
    final static Logger logger = LoggerFactory.getLogger(BaseProjecttemplateController.class);
    /**
     * 服务对象
     */
    @Resource
    private BaseProjecttemplateService projecttemplateService;
    @Resource
    private SysDictInfoService sysDictInfoService;
    @Resource
    private BaseProjecttypeService projecttypeService;
    @Resource
    private BaseProjectbudgetService baseProjectbudgetService;
    @Resource
    private SysUserInfoService sysUserInfoService;


    /**
     * 页面初始化
     *
     * @return
     */
    @ApiOperation(value = "页面初始化", notes = "页面初始化")
    @RequestMapping(value = "/init", method = RequestMethod.GET)
    public String init(Model model) {
        //审批状态
        List<Map<String, String>> dictList = sysDictInfoService.queryByCode("approve_status");
        model.addAttribute("dictList", dictList);
        //项目类型
        List<Map<String, String>> xmlxList = projecttypeService.getXMLX();
        model.addAttribute("xmlxList", xmlxList);
        //创建人
        List<Map<String, String>> cjrList = sysUserInfoService.getCJR();
        model.addAttribute("cjrList", cjrList);

        return "baseprojecttemplate/list";
    }

    @LogAround("数据分页查询")
    @ApiOperation(value = "数据分页查询", notes = "数据分页查询")
    @ResponseBody
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    public BaseResult list(@RequestParam("page") Integer pageNum,
                           @RequestParam("limit") Integer pageSize,
                           @RequestParam(required = false, defaultValue = "", value = "ZWMC") String zwmc,
                           @RequestParam(required = false, defaultValue = "", value = "ERPBH") String erpbh,
                           @RequestParam(required = false, defaultValue = "", value = "SPZT") String spzt,
                           @RequestParam(required = false, defaultValue = "", value = "XMLX") String xmlx,
                           @RequestParam(required = false, defaultValue = "", value = "CJR") String cjr,
                           @RequestParam(required = false, defaultValue = "", value = "sortName") String sortName,
                           @RequestParam(required = false, defaultValue = "", value = "sortOrder") String sortOrder) {
        PageHelper.startPage(pageNum, pageSize);

        Map<String, String> paraMap = new HashMap<>();
        paraMap.put("sortName", sortName);
        paraMap.put("sortOrder", sortOrder);
        paraMap.put("zwmc", zwmc);
        paraMap.put("erpbh", erpbh);
        paraMap.put("spzt", spzt);
        paraMap.put("xmlx", xmlx);
        paraMap.put("cjr", cjr);

        List<BaseProjecttemplate> list = projecttemplateService.getTemplateList(paraMap);

        PageInfo<BaseProjecttemplate> pageinfo = new PageInfo<>(list);
        //取出查询结果
        List<BaseProjecttemplate> 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 = "/forAdd", method = RequestMethod.GET)
    public String forAdd(Model model) {
        //审批状态
        List<Map<String, String>> dictList = sysDictInfoService.queryByCode("approve_status");
        model.addAttribute("dictList", dictList);
        //项目类型
        List<Map<String, String>> xmlxList = projecttypeService.getXMLX();
        model.addAttribute("xmlxList", xmlxList);
        //学科分类
        List<Map<String, String>> xkflList = projecttypeService.getXMLX();
        model.addAttribute("xkflList", xkflList);

        return "baseprojecttemplate/add";
    }

    /**
     * 保存数据
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/doAdd", method = RequestMethod.POST)
    public BaseResult doAdd(BaseProjecttemplate projecttemplate) {
        projecttemplate.setCjr(getSysUserId());
        if (projecttemplate.getSfgjmmjsxm() == null)
            projecttemplate.setSfgjmmjsxm(false);

        int num = projecttemplateService.insert(projecttemplate);
        if (num > 0) {
            return BaseResult.success("保存成功!", projecttemplate);
        } else if (num == -1) {
            return BaseResult.failure("该模板已存在，如需修改信息，请选中模板进行编辑!");
        } else {
            return BaseResult.failure("保存失败!");
        }
    }

    /**
     * 批量删除
     *
     * @param data
     * @return
     */
    @ApiOperation(value = "批量删除项目模板", notes = "批量删除项目模板")
    @RequestMapping(value = "/batchDelete", method = RequestMethod.POST)
    @ResponseBody
    public BaseResult batchDelete(@RequestBody String data) {
        if (logger.isInfoEnabled()) {
            logger.info("batchDelete -- data：" + data);
        }
        List<BaseProjecttemplate> prjList = JsonTool.parseArray(data, BaseProjecttemplate.class);
        List<String> idList = new ArrayList<>();
        prjList.forEach(item -> {
            idList.add(item.getId().toString());
        });
        if (logger.isInfoEnabled()) {
            logger.info("batchDelete -- idList：{}", idList);
        }

        int num = projecttemplateService.batchDelete(idList);
        if (num > 0) {
            return BaseResult.success("批量删除成功!");
        } else {
            return BaseResult.failure("批量删除失败!");
        }
    }

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

    /**
     * 查看页面
     *
     * @param model
     * @param id
     * @return
     */
    @RequestMapping(value = "/forView/{id}", method = RequestMethod.GET)
    public String forView(Model model, @PathVariable Integer id) {
        BaseProjecttemplate prjTemplate = projecttemplateService.queryById(id);
        model.addAttribute("template", prjTemplate);

        return "baseprojecttemplate/view";
    }

    /**
     * 编辑页面
     *
     * @param model
     * @param id
     * @return
     */
    @RequestMapping(value = "/forUpdate/{id}", method = RequestMethod.GET)
    public String forUpdate(Model model, @PathVariable Integer id) {
        BaseProjecttemplate prjTemplate = projecttemplateService.queryById(id);
        model.addAttribute("template", prjTemplate);
        return "baseprojecttemplate/update";
    }

    @RequestMapping(value = "/doUpdate", method = RequestMethod.POST)
    @ResponseBody
    public BaseResult doUpdate(BaseProjecttemplate projecttemplate) {
        if (projecttemplate.getSfgjmmjsxm() == null)
            projecttemplate.setSfgjmmjsxm(false);

        int num = projecttemplateService.update(projecttemplate);
        if (num > 0) {
            return BaseResult.success("修改成功！");
        } else {
            return BaseResult.failure("修改失败！");
        }
    }

}

