package com.liang.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.liang.common.base.BaseResult;
import com.liang.common.JsonTool;
import com.liang.common.base.BaseController;
import com.liang.entity.BaseCompany;
import com.liang.service.BaseCompanyService;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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;

/**
 * (BaseCompany)表控制层
 *
 * @author makejava
 * @since 2023-02-15 23:13:17
 */
@Controller
@RequestMapping("baseCompany")
public class BaseCompanyController extends BaseController {
    final static Logger logger = LoggerFactory.getLogger(com.liang.controller.BaseCompanyController.class);

    /**
     * 服务对象
     */
    @Resource
    private BaseCompanyService baseCompanyService;

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

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

    /**
     * 删除数据
     *
     * @return 删除是否成功
     */
    @ApiOperation(value = "删除一个系统用户", notes = "删除一个系统用户")
    @RequestMapping(value = "/delete/{id}", method = RequestMethod.GET)
    @ResponseBody
    public BaseResult delete(@PathVariable Integer id) {
        boolean flag = baseCompanyService.deleteById(id);
        if (flag) {
            return BaseResult.success("删除成功!");
        } else {
            return BaseResult.failure("删除失败!");
        }
    }

    @ApiOperation(value = "页面初始化", notes = "页面初始化")
    @RequestMapping(value = "/init", method = RequestMethod.GET)
    public String init() {
        return "basecompany/list";
    }

    /**
     * 分页查询
     *
     * @param pageNum
     * @param pageSize
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public BaseResult list(@RequestParam("page") Integer pageNum,
                           @RequestParam("limit") Integer pageSize,
                           @RequestParam(required = false, defaultValue = "", value = "MC") String mc,
                           @RequestParam(required = false, defaultValue = "", value = "DWQC") String dwqc) {
        PageHelper.startPage(pageNum, pageSize);

        Map<String, String> paraMap = new HashMap<>();
        paraMap.put("mc", mc);
        paraMap.put("dwqc", dwqc);

        List<BaseCompany> list = baseCompanyService.getBaseCompanyList(paraMap);

        PageInfo<BaseCompany> pageinfo = new PageInfo<>(list);
        // 取出查询结果
        List<BaseCompany> 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) {
        return "basecompany/add";
    }

    /**
     * 新建单位
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/doAdd", method = RequestMethod.POST)
    public BaseResult doAdd(BaseCompany baseCompany) {
        baseCompany.setCjr(getSysUserId());
        String dwqc = baseCompany.getDwqc();
        int num = baseCompanyService.insert(baseCompany);
        if (num > 0) {
            return BaseResult.success("新建单位'" + dwqc + "'成功!");
        } else if (num == -1) {
            return BaseResult.failure("新建单位'" + dwqc + "'失败，该单位已存在!");
        } else {
            return BaseResult.failure("新建单位'" + dwqc + "'失败!");
        }
    }

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

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

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

        BaseCompany baseCompany = baseCompanyService.queryById(id);
        if (baseCompany.getLx() == null)
            baseCompany.setLxmc("");
        else if (baseCompany.getLx() == 1)
            baseCompany.setLxmc("内部单位");
        else if (baseCompany.getLx() == 2)
            baseCompany.setLxmc("外部单位");
        model.addAttribute("baseCompany", baseCompany);
        return "basecompany/update";
    }

    @ResponseBody
    @RequestMapping(value = "/doUpdate", method = RequestMethod.POST)
    public BaseResult doUpdate(BaseCompany baseCompany) {
        String dwqc = baseCompany.getDwqc();
        int num = baseCompanyService.update(baseCompany);
        if (num > 0) {
            return BaseResult.success("编辑单位'" + dwqc + "'成功!");
        } else {
            return BaseResult.failure("编辑单位'" + dwqc + "'失败!");
        }
    }

    @ApiOperation(value = "查看页面初始化", notes = "查看页面初始化")
    @RequestMapping(value = "/forView/{id}", method = RequestMethod.GET)
    public String forView(Model model, @PathVariable Integer id) {

        BaseCompany baseCompany = baseCompanyService.queryById(id);
        if (baseCompany.getLx() == null)
            baseCompany.setLxmc("");
        else if (baseCompany.getLx() == 1)
            baseCompany.setLxmc("内部单位");
        else if (baseCompany.getLx() == 2)
            baseCompany.setLxmc("外部单位");
        model.addAttribute("baseCompany", baseCompany);
        return "basecompany/view";
    }

    @ResponseBody
    @RequestMapping(value = "/dwlist", method = RequestMethod.GET)
    public Object dwlist(@RequestParam("page") Integer pageNum,
                         @RequestParam("limit") Integer pageSize,
                         @RequestParam(required = false, defaultValue = "", value = "mc") String mc) {
        PageHelper.startPage(pageNum, pageSize);
        Map<String, String> paraMap = new HashMap<>();
        paraMap.put("mc", mc);
        List<BaseCompany> list = baseCompanyService.getBaseCompanyList(paraMap);

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

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

        BaseCompany baseCompany = baseCompanyService.queryById(id);

        Map<String, String> paraMap = new HashMap<>();
        paraMap.put("mc", mc);
        paraMap.put("fbm", baseCompany.getBm());
        List<BaseCompany> list = baseCompanyService.getBaseCompanyList(paraMap);

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

}

