package com.nantian.cloud.jljc.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nantian.cloud.jljc.entity.TSupervisionOrganization;
import com.nantian.cloud.jljc.entity.TSupervisionPerson;
import com.nantian.cloud.jljc.service.TSupervisionOrganizationService;
import com.nantian.cloud.jljc.service.TSupervisionPersonService;
import com.nantian.sc.cloud.common.core.util.R;
import com.nantian.sc.cloud.common.log.annotation.SysLog;
import com.nantian.sc.cloud.common.security.service.ScUser;
import com.nantian.sc.cloud.common.security.util.SecurityUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@CrossOrigin
@RestController
@AllArgsConstructor
@RequestMapping("/TSupervisionPerson")
@Api(value = "t_supervision_person",tags = "监察人员")
public class TSupervisionPersonController {

    @Autowired
    TSupervisionPersonService tSupervisionPersonService;
    @Autowired
    TSupervisionOrganizationService tSupervisionOrganizationService;

    /**
     * 查询
     * @param page
     * @param tSupervisionPerson
     * @return
     */
    @GetMapping("/selectall")
    @SysLog("分页查询监察人员信息")
    @ApiOperation(value = "分页查询监察人员信息", notes = "分页查询监察人员信息")
    @ApiImplicitParam(name = "tSupervisionPerson", value = "tSupervisionPerson", required = true, dataType = "json", paramType = "path")
    public R selectall(Page page, TSupervisionPerson tSupervisionPerson) {
        tSupervisionPerson.setDelFlag("1"); //筛选未删除项
        return R.ok(tSupervisionPersonService.selectall(page,tSupervisionPerson));
    }

    /**
     * 根据ID数组查询监察编号，防止前端显示顺序错乱
     * @return
     */
    @GetMapping("/selectByIds")
    @SysLog("根据ID数组查询监察编号，防止前端显示顺序错乱")
    public R selectByIds(String ids){
        String[] split = ids.split(",");
        List<String> codeList = new ArrayList<>();
        for (int i = 0; i < split.length; i++) {
            TSupervisionPerson supervisionPerson = tSupervisionPersonService.getById(Integer.parseInt(split[i]));
            codeList.add(supervisionPerson.getCode());
        }
        return R.ok(codeList);
    }

    /**
     * 获取监察人员信息
     */
    @GetMapping("/getList")
    @SysLog("获取监察人员信息")
    @ApiOperation(value = "获取监察人员信息", notes = "获取监察人员信息")
    @ApiImplicitParam(name = "tSupervisionPerson", value = "tSupervisionPerson", required = true, dataType = "json", paramType = "path")
    public R getList(TSupervisionPerson tSupervisionPerson) {
        return R.ok(tSupervisionPersonService.list(Wrappers.query(tSupervisionPerson)));
    }

    /**
     * 根据监察机构所属区域获取监察人员信息
     */
    @GetMapping("/getListByOrgRegion")
    @SysLog("根据监察机构所属区域获取监察人员信息")
    @ApiOperation(value = "根据监察机构所属区域获取监察人员信息", notes = "根据监察机构所属区域获取监察人员信息")
    @ApiImplicitParam(name = "tSupervisionPerson", value = "tSupervisionPerson", required = true, dataType = "json", paramType = "path")
    public R getListByOrgRegion(String organizationid) {
        return R.ok(tSupervisionPersonService.getListByOrgRegion(organizationid));
    }

    /**
     * 获取区域、监察人员树形结构
     * @return
     */
    @GetMapping("/regionPersonTree")
    @SysLog("获取区域、监察人员树形结构")
    @ApiOperation(value = "获取区域、监察人员树形结构", notes = "获取区域、监察人员树形结构")
    @ApiImplicitParam(name = "tSupervisionPerson", value = "tSupervisionPerson", required = true, dataType = "json", paramType = "path")
    public R regionPersonTree(TSupervisionPerson tSupervisionPerson) {
        tSupervisionPerson.setDelFlag("1"); //筛选未删除项
        return R.ok(tSupervisionPersonService.regionPersonTree(tSupervisionPerson));
    }

    /**
     * 查询本级监察机构的监察人员2
     */
    @GetMapping("/getByOrg2")
    @SysLog("查询本级监察机构的监察人员2")
    @ApiOperation(value = "查询本级监察机构的监察人员2", notes = "查询本级监察机构的监察人员2")
    public R getByOrg() {
        ScUser u = SecurityUtils.getUser();
        Integer depetId = u.getDeptId();
        TSupervisionOrganization organization = new TSupervisionOrganization();
        organization.setAreaCode(depetId+"");
        List<TSupervisionOrganization> organizationList = tSupervisionOrganizationService.list(Wrappers.query(organization));
        if(organizationList.size()==0){
            return R.ok();
        }else{
            List<String> idList = new ArrayList<String>();
            for (TSupervisionOrganization tOrganization : organizationList) {
                idList.add(tOrganization.getId()+"");
            }
            String[] ids = new String[idList.size()];
            idList.toArray(ids);
            return R.ok(tSupervisionPersonService.getByOrg(ids));
        }

    }

    /**
     * 查询本级监察机构的监察人员
     */
    @GetMapping("/getByOrg")
    @SysLog("查询本级监察机构的监察人员")
    @ApiOperation(value = "查询本级监察机构的监察人员", notes = "查询本级监察机构的监察人员")
    public R getJCPerson() {
        ScUser u = SecurityUtils.getUser();
        return R.ok(tSupervisionPersonService.getJCPerson(u.getDeptId()));
    }

    /**
     * 新增
     * @param
     * @return
     */
    @PostMapping("/insertall")
    @SysLog("添加监察人员信息")
    @ApiOperation(value = "添加监察人员信息", notes = "添加监察人员信息")
    @ApiImplicitParam(name = "tStandardsRegulations", value = "tStandardsRegulations", required = true, dataType = "boolean", paramType = "path")
    public Boolean insertall(@RequestBody TSupervisionPerson tSupervisionPerson){
        ScUser u = SecurityUtils.getUser();
        tSupervisionPerson.setDelFlag("1");
        tSupervisionPerson.setUserid(u.getId());
        return tSupervisionPersonService.insertall(tSupervisionPerson);
    }

    /**
     * 修改方法
     * @param
     * @return
     */
    @PostMapping("/updateall")
    @SysLog("修改监察人员信息")
    @ApiOperation(value = "修改监察人员信息", notes = "修改监察人员信息")
    @ApiImplicitParam(name = "TSupervisionOrganization", value = "TSupervisionOrganization", required = true, dataType = "boolean", paramType = "path")
    public Boolean updateall(@RequestBody TSupervisionPerson tSupervisionPerson){
        ScUser u = SecurityUtils.getUser();
        tSupervisionPerson.setUserid(u.getId());
        return tSupervisionPersonService.updateall(tSupervisionPerson);
    }

    /**
     * 删除监察人员
     * @param
     * @return
     */
    @DeleteMapping("/deleteall")
    @SysLog("删除监察人员")
    @ApiOperation(value = "删除监察人员", notes = "删除监察人员")
    @ApiImplicitParam(name = "tSupervisionPerson", value = "tSupervisionPerson", required = true, dataType = "boolean", paramType = "path")
    public Boolean deleteall(@RequestBody TSupervisionPerson tSupervisionPerson){
        ScUser u = SecurityUtils.getUser();
        tSupervisionPerson.setUserid(u.getId());
        tSupervisionPerson.setDelFlag("4");
        return tSupervisionPersonService.updateById(tSupervisionPerson);
    }

    /**
     * 批量新增监察是否已经存在相同数据
     */
    @ApiOperation(value = "批量新增监察是否已经存在相同数据")
    @PostMapping("/checkname")
    @SysLog("批量新增监察是否已经存在相同数据")
    public List<String> checkallname(@RequestParam MultipartFile file) throws IOException {
        return tSupervisionPersonService.checke_allname(file,tSupervisionPersonService);
    }

    /**
     * 监察人员批量新增 不覆盖 直接跳过
     * @param
     * @return
     * @throws
     */
    @PostMapping("/inssertallexcel")
    @SysLog("批量新增监察人员信息(不覆盖)")
    @ApiOperation(value = "批量新增监察人员信息(不覆盖)")
    public R batchAdd(@RequestParam MultipartFile file) throws IOException {
        boolean flag=tSupervisionPersonService.importbyexcel(file,tSupervisionPersonService);
        if(flag){
            return R.ok();
        }else {
            return R.failed();
        }
    }

    /**
     * 监察人员批量新增  覆盖并添加
     * @param
     * @return
     * @throws
     */
    @PostMapping("/inssertallexcelf")
    @SysLog("批量新增监察人员信息(覆盖)")
    @ApiOperation(value = "批量新增监察人员信息(覆盖)")
    public R batchAddf(@RequestParam MultipartFile file) throws IOException {
        boolean flag=tSupervisionPersonService.importbyexcelf(file,tSupervisionPersonService);
        if(flag){
            return R.ok();
        }else {
            return R.failed();
        }
    }

    /**
     * 导出批量新增模板
     */
    @ApiOperation(value = "监察人员信息导出批量新增模板")
    @PostMapping(value = "/outputexcel")
    @SysLog("监察人员信息导出批量新增模板")
    public void outputmodel(HttpServletRequest request, HttpServletResponse response) throws IOException {
        tSupervisionPersonService.outpute(request,response);
    }

}
