package com.liang.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.liang.common.JsonTool;
import com.liang.common.base.BaseResult;
import com.liang.common.annotation.LogAround;
import com.liang.common.base.BaseController;
import com.liang.entity.*;
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.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.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * (BasePerson)表控制层
 *
 * @author makejava
 * @since 2023-02-15 23:13:18
 */
@Controller
@RequestMapping("basePerson")
public class BasePersonController extends BaseController {
    final static Logger logger = LoggerFactory.getLogger(com.liang.controller.BasePersonController.class);
    /**
     * 服务对象
     */
    @Resource
    private BasePersonService basePersonService;
    @Resource
    private BasePartyService basePartyService;
    @Resource
    private BaseNationService baseNationService;
    @Resource
    private BaseEducationService baseEducationService;
    @Resource
    private BaseDegreeService baseDegreeService;
    @Resource
    private BaseLanguageService baseLanguageService;
    @Resource
    private SysDictInfoService sysDictInfoService;
    @Resource
    private BaseAreaService baseAreaService;
    @Resource
    private BaseSpecialityService baseSpecialityService;


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

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

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

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

    /**
     * 单位管理页面分页查询
     *
     * @param pageNum
     * @param pageSize
     * @return
     */
    @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 = "dwmc") String dwmc,
                           @RequestParam(required = false, defaultValue = "", value = "xm") String xm,
                           @RequestParam(required = false, defaultValue = "", value = "bmmc") String bmmc,
                           @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("dwmc", dwmc);
        paraMap.put("xm", xm);
        paraMap.put("bmmc", bmmc);
        List<BasePerson> list = basePersonService.getBasePersonList(paraMap);

        PageInfo<BasePerson> pageinfo = new PageInfo<>(list);
        // 取出查询结果
        List<BasePerson> 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 pageNum
     * @param pageSize
     * @param xm
     * @param dwmc
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/rylist", method = RequestMethod.GET)
    public Object rylist(@RequestParam("page") Integer pageNum,
                         @RequestParam("limit") Integer pageSize,
                         @RequestParam(required = false, defaultValue = "", value = "xm") String xm,
                         @RequestParam(required = false, defaultValue = "", value = "dwmc") String dwmc,
                         @RequestParam(required = false, defaultValue = "", value = "bmmc") String bmmc) {
        PageHelper.startPage(pageNum, pageSize);

        Map<String, String> paraMap = new HashMap<>();
        paraMap.put("xm", xm);
        paraMap.put("dwmc", dwmc);
        paraMap.put("bmmc", bmmc);
        List<BasePerson> list = basePersonService.getBasePersonList(paraMap);

        PageInfo<BasePerson> 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", method = RequestMethod.GET)
    public String forAdd(Model model) {
        //民族
        List<Map<String, String>> mzList = baseNationService.getNation();
        model.addAttribute("mzList", mzList);
        //党派
        List<Map<String, String>> dpList = basePartyService.getParty();
        model.addAttribute("dpList", dpList);
        //何国华侨**授予国家
        List<Map<String, String>> gjList = baseAreaService.getCountry();
        model.addAttribute("gjList", gjList);
        //学历
        List<Map<String, String>> xlList = baseEducationService.getEducationList();
        model.addAttribute("xlList", xlList);
        //学位
        List<Map<String, String>> xwList = baseDegreeService.getDegreeList();
        model.addAttribute("xwList", xwList);
        //专业专长
        List<Map<String, String>> zyzcList = baseSpecialityService.getSpeciality();
        model.addAttribute("zyzcList", zyzcList);
        //外语语种
        List<Map<String, String>> wyyzList = baseLanguageService.getLanguageList();
        model.addAttribute("wyyzList", wyyzList);
        //熟练程度
        List<Map<String, String>> slcdList = sysDictInfoService.queryByCode("proficiency_level");
        model.addAttribute("slcdList", slcdList);
        //院士
        List<Map<String, String>> ysList = sysDictInfoService.queryByCode("academician_type");
        model.addAttribute("ysList", ysList);

        return "baseperson/add";
    }

    /**
     * 新建人员
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/doAdd", method = RequestMethod.POST)
    public BaseResult doAdd(BasePerson basePerson) {

        basePerson.setCjr(getSysUserId());
        String xm = basePerson.getXm();
        int num = basePersonService.insert(basePerson);
        if (num > 0) {
            return BaseResult.success("新建人员'" + xm + "'成功!");
        } else if (num == -1) {
            return BaseResult.failure("新建人员'" + xm + "'失败，该人员已存在!");
        } else {
            return BaseResult.failure("新建人员'" + xm + "'失败!");
        }
    }

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

        BasePerson basePerson = basePersonService.queryById(id);
        model.addAttribute("basePerson", basePerson);
        return "baseperson/view";
    }

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

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

    /**
     * 删除数据
     *
     * @return 删除是否成功
     */
    @RequestMapping(value = "/delete/{id}", method = RequestMethod.GET)
    @ResponseBody
    public BaseResult delete(@PathVariable Integer id) {
        boolean flag = basePersonService.deleteById(id);
        if (flag) {
            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) {
        //人员信息
        BasePerson basePerson = basePersonService.queryById(id);
        //民族
        List<Map<String, String>> mzList = baseNationService.getNation();
        model.addAttribute("mzList", mzList);
        //党派
        List<Map<String, String>> dpList = basePartyService.getParty();
        model.addAttribute("dpList", dpList);
        //何国华侨**授予国家
        List<Map<String, String>> gjList = baseAreaService.getCountry();
        model.addAttribute("gjList", gjList);
        //学历
        List<Map<String, String>> xlList = baseEducationService.getEducationList();
        model.addAttribute("xlList", xlList);
        //学位
        List<Map<String, String>> xwList = baseDegreeService.getDegreeList();
        model.addAttribute("xwList", xwList);
        //外语语种
        List<Map<String, String>> wyyzList = baseLanguageService.getLanguageList();
        model.addAttribute("wyyzList", wyyzList);
        //熟练程度
        List<Map<String, String>> slcdList = sysDictInfoService.queryByCode("proficiency_level");
        model.addAttribute("slcdList", slcdList);
        //院士
        List<Map<String, String>> ysList = sysDictInfoService.queryByCode("academician_type");
        model.addAttribute("ysList", ysList);

        List<Map<String, String>> zyzcList = baseSpecialityService.getSpeciality();
        //专业专长
//        if(basePerson.getZyzc() != null && basePerson.getZyzc() != "") {
//            Map<String, String> map = new HashMap<>();
//            map.put("name", basePerson.getZyzc());
//
//            if (!zyzcList.contains(map)) {
//                zyzcList.add(map);
//            }
//        }
        model.addAttribute("zyzcList", zyzcList);

        if (basePerson.getXb() != null) {
            if (basePerson.getXb().equals("male"))
                basePerson.setXb("男");
            else if (basePerson.getXb().equals("female"))
                basePerson.setXb("女");
        }
        if (basePerson.getBsh() != null) {
            if (basePerson.getBsh().equals("T"))
                basePerson.setBsh("是");
            else if (basePerson.getBsh().equals("F"))
                basePerson.setBsh("否");
        }
        model.addAttribute("basePerson", basePerson);

        return "baseperson/update";
    }

    @ResponseBody
    @RequestMapping(value = "/doUpdate", method = RequestMethod.POST)
    public BaseResult doUpdate(BasePerson basePerson) {

        String xm = basePerson.getXm();
        int num = basePersonService.update(basePerson);
        if (num > 0) {
            BaseSpeciality baseSpeciality = new BaseSpeciality();
            baseSpeciality.setMc(basePerson.getZyzc());
            baseSpecialityService.insert(baseSpeciality);
            return BaseResult.success("编辑人员'" + xm + "'成功!");
        } else {
            return BaseResult.failure("编辑人员'" + xm + "'失败!");
        }
    }

    @ResponseBody
    @RequestMapping(value = "/dwrylist/{dw}", method = RequestMethod.GET)
    public Object dwrylist(@PathVariable Integer dw,
                           @RequestParam("page") Integer pageNum,
                           @RequestParam("limit") Integer pageSize,
                           @RequestParam(required = false, defaultValue = "", value = "xm") String xm) {
        PageHelper.startPage(pageNum, pageSize);
        List<BasePerson> list = basePersonService.getPersonListByDw(dw, xm);

        PageInfo<BasePerson> 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;
    }
}

