package com.rwk.web.controller.ability;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import com.rwk.common.utils.DateUtils;
import com.rwk.common.utils.StringUtils;
import com.rwk.system.domain.RwkMeeting;
import com.rwk.system.domain.RwkMeetingStaff;
import com.rwk.system.service.IRwkMeetingService;
import com.rwk.system.service.IRwkMeetingStaffService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.rwk.common.annotation.Log;
import com.rwk.common.enums.BusinessType;
import com.rwk.system.domain.RwkStaffInformation;
import com.rwk.system.service.IRwkStaffInformationService;
import com.rwk.common.core.controller.BaseController;
import com.rwk.common.core.domain.AjaxResult;
import com.rwk.common.utils.poi.ExcelUtil;
import com.rwk.common.core.page.TableDataInfo;

/**
 * 人员信息Controller
 * 
 * @author rick
 * @date 2024-11-06
 */
@Controller
@RequestMapping("/system/information")
public class RwkStaffInformationController extends BaseController
{
    private String prefix = "system/information";

    @Autowired
    private IRwkStaffInformationService rwkStaffInformationService;

    //人员培训记录表
    @Autowired
    private IRwkMeetingStaffService rwkMeetingStaffService;

    //会议内容表
    @Autowired
    private IRwkMeetingService rwkMeetingService;

    @RequiresPermissions("system:information:view")
    @GetMapping()
    public String information()
    {
        return prefix + "/information";
    }


    /**
     * 查看收文
     */
    @GetMapping("/informationView/{id}")
    public String receiveView(@PathVariable("id") Long id, ModelMap mmap)
    {
        RwkStaffInformation rwkStaffInformation = rwkStaffInformationService.selectRwkStaffInformationById(id);
        List<List<String>> resumeLines = splitConversion(rwkStaffInformation.getResume());
        List<List<String>> trainingLines = splitConversion(rwkStaffInformation.getTraining());
        List<List<String>> achievementLines = splitConversion(rwkStaffInformation.getAchievement());
        mmap.put("rwkStaffInformation",rwkStaffInformation);
        mmap.put("resumeLines",resumeLines);
        mmap.put("trainingLines",trainingLines);
        mmap.put("achievementLines",achievementLines);
        //获取当前人员的培训记录消息
        getTrainingRecord(rwkStaffInformation,mmap);
        return prefix + "/informationView";
    }

    /**
     * 查询人员信息列表
     */
    @RequiresPermissions("system:information:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(RwkStaffInformation rwkStaffInformation)
    {
        startPage();
        List<RwkStaffInformation> list = rwkStaffInformationService.selectRwkStaffInformationList(rwkStaffInformation);
        return getDataTable(list);
    }

    /**
     * 导出人员信息列表
     */
    @RequiresPermissions("system:information:export")
    @Log(title = "人员信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(RwkStaffInformation rwkStaffInformation)
    {
        List<RwkStaffInformation> list = rwkStaffInformationService.selectRwkStaffInformationList(rwkStaffInformation);
        ExcelUtil<RwkStaffInformation> util = new ExcelUtil<RwkStaffInformation>(RwkStaffInformation.class);
        return util.exportExcel(list, "人员信息数据");
    }

    /**
     * 新增人员信息
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存人员信息
     */
    @RequiresPermissions("system:information:add")
    @Log(title = "人员信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(RwkStaffInformation rwkStaffInformation)
    {
        rwkStaffInformation.setCreateTime(DateUtils.getNowDate());
        rwkStaffInformation.setCreateBy(getSysUser().getLoginName());
        return toAjax(rwkStaffInformationService.insertRwkStaffInformation(rwkStaffInformation));
    }

    /**
     * 修改人员信息
     */
    @RequiresPermissions("system:information:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        RwkStaffInformation rwkStaffInformation = rwkStaffInformationService.selectRwkStaffInformationById(id);
        mmap.put("rwkStaffInformation", rwkStaffInformation);
        return prefix + "/edit";
    }

    /**
     * 修改保存人员信息
     */
    @RequiresPermissions("system:information:edit")
    @Log(title = "人员信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(RwkStaffInformation rwkStaffInformation)
    {
        rwkStaffInformation.setUpdateTime(DateUtils.getNowDate());
        rwkStaffInformation.setUpdateBy(getSysUser().getLoginName());
        return toAjax(rwkStaffInformationService.updateRwkStaffInformation(rwkStaffInformation));
    }

    /**
     * 删除人员信息
     */
    @RequiresPermissions("system:information:remove")
    @Log(title = "人员信息", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(rwkStaffInformationService.deleteRwkStaffInformationByIds(ids));
    }

    /**
     * 首页大屏数据集合
     */
    @GetMapping("/dashboard")
    @ResponseBody
    public AjaxResult dashboard(ModelMap mmap) {
        List<Map<String, Object>> list1 = rwkStaffInformationService.selectTotalByJob();
        List<Map<String, Object>> list2 = rwkStaffInformationService.selectTotalByParty();
        List<Map<String, Object>> list3 = rwkStaffInformationService.selectTotalByAge();
        List<Map<String, Object>> list4 = rwkStaffInformationService.selectTotalByUnitAttribute();
        List<Map<String, Object>> list5 = rwkStaffInformationService.selectTotalByQrzQualification();
        List<Map<String, Object>> list6 = rwkStaffInformationService.selectTotalByQrzDegree();
        List<Map<String, Object>> list7 = rwkStaffInformationService.selectTotalByZzQualification();
        List<Map<String, Object>> list8 = rwkStaffInformationService.selectTotalByZzDegree();
        mmap.put("list1",list1);
        mmap.put("list2",list2);
        mmap.put("list3",list3);
        mmap.put("list4",list4);
        mmap.put("list5",list5);
        mmap.put("list6",list6);
        mmap.put("list7",list7);
        mmap.put("list8",list8);
        return success(mmap);
    }

    //对数据换行符进行处理
    private List<List<String>> splitConversion(String string){
        List<List<String>> result = new ArrayList<>();
        if (StringUtils.isNotEmpty(string)){
            String[] lines = string.split("\r\n");
            for (String line : lines) {
                // 对每一行按空白字符分割，去除前后空格
                String[] parts = line.trim().split("\\s+");
                List<String> lineParts = new ArrayList<>();
                for (String part : parts) {
                    if (!part.trim().isEmpty()) {
                        lineParts.add(part.trim());
                    }
                }
                if (!lineParts.isEmpty()) {
                    result.add(lineParts);
                }
            }
        }
        return result;
    }

    //获取当前人员的培训记录
    private void getTrainingRecord(RwkStaffInformation rwkStaffInformation,ModelMap mmap) {
        RwkMeetingStaff rwkMeetingStaff = new RwkMeetingStaff();
        rwkMeetingStaff.setPersonnelId(rwkStaffInformation.getId().toString());
        rwkMeetingStaff.setIsSign(1L);
        List<RwkMeeting> rwkMeetingList = new ArrayList<>();
        List<RwkMeetingStaff> rwkMeetingStaffList = rwkMeetingStaffService.selectRwkMeetingStaffList(rwkMeetingStaff);
        for (RwkMeetingStaff rwkMeetingStaff1 : rwkMeetingStaffList) {
            RwkMeeting rwkMeeting = rwkMeetingService.selectRwkMeetingById(rwkMeetingStaff1.getMeetingId());
            rwkMeetingList.add(rwkMeeting);
        }
        mmap.put("rwkMeetingList", rwkMeetingList);
    }
}
