package org.jeecg.modules.zrzyqc.qywtfk.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.PermissionData;
import org.jeecg.common.system.api.ISysBaseAPI;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.SpringContextUtils;
import org.jeecg.common.util.oConvertUtils;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.modules.zrzyqc.qywtfk.entity.QcQywtfk;
import org.jeecg.modules.zrzyqc.qywtfk.service.IQcQywtfkService;
import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;

 /**
 * @Description: 企业问题反馈
 * @Author: jeecg-boot
 * @Date:   2022-08-17
 * @Version: V1.0
 */
@Api(tags="企业问题反馈")
@RestController
@RequestMapping("/qcsb.qywtfk/qcQywtfk")
@Slf4j
public class QcQywtfkController extends JeecgController<QcQywtfk, IQcQywtfkService> {
	@Autowired
	private IQcQywtfkService qcQywtfkService;
	/**
	 * 分页列表查询
	 *
	 * @param qcQywtfk
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	//@AutoLog(value = "企业问题反馈-分页列表查询")
	@ApiOperation(value="企业问题反馈-分页列表查询", notes="企业问题反馈-分页列表查询")
	@GetMapping(value = "/list")
	@PermissionData(pageComponent = "qcsb/qywtfk/QcQywtfkList")
	public Result<IPage<QcQywtfk>> queryPageList(QcQywtfk qcQywtfk,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<QcQywtfk> queryWrapper = QueryGenerator.initQueryWrapper(qcQywtfk, req.getParameterMap());
		Page<QcQywtfk> page = new Page<QcQywtfk>(pageNo, pageSize);
		queryWrapper.notIn("wtlx","企业注册");
		IPage<QcQywtfk> pageList = qcQywtfkService.page(page, queryWrapper);
		return Result.OK(pageList);
	}

	 /**
	  * 分页列表查询
	  *
	  * @param qcQywtfk
	  * @param pageNo
	  * @param pageSize
	  * @param req
	  * @return
	  */
	 //@AutoLog(value = "企业问题反馈-分页列表查询")
	 @ApiOperation(value="企业问题反馈-分页列表查询", notes="企业问题反馈-分页列表查询")
	 @GetMapping(value = "/list_qyzc")

	 public Result<IPage<QcQywtfk>> queryPageList_qyzc(QcQywtfk qcQywtfk,
												  @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
												  @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
												  HttpServletRequest req) {
		 QueryWrapper<QcQywtfk> queryWrapper = QueryGenerator.initQueryWrapper(qcQywtfk, req.getParameterMap());
		 Page<QcQywtfk> page = new Page<QcQywtfk>(pageNo, pageSize);
		 List<Integer> sbzts=new ArrayList<>();
		 sbzts.add(1);
		 sbzts.add(2);
		 queryWrapper.in("wtzt",sbzts);
		 queryWrapper.eq("wtlx","企业注册");
		 IPage<QcQywtfk> pageList = qcQywtfkService.page(page, queryWrapper);
		 return Result.OK(pageList);
	 }
	 /**
	  * 分页列表查询
	  *
	  * @param qcQywtfk
	  * @param pageNo
	  * @param pageSize
	  * @param req
	  * @return
	  */
	 //@AutoLog(value = "企业问题反馈-分页列表查询")
	 @ApiOperation(value="企业问题反馈-分页列表查询", notes="企业问题反馈-分页列表查询")
	 @GetMapping(value = "/list_qyzc2")
	 public Result<IPage<QcQywtfk>> queryPageList_qyzc2(QcQywtfk qcQywtfk,
												   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
												   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
												   HttpServletRequest req) {
		 QueryWrapper<QcQywtfk> queryWrapper = QueryGenerator.initQueryWrapper(qcQywtfk, req.getParameterMap());
		 Page<QcQywtfk> page = new Page<QcQywtfk>(pageNo, pageSize);
		 List<Integer> sbzts=new ArrayList<>();
		 sbzts.add(3);
		 sbzts.add(4);
		 queryWrapper.in("wtzt",sbzts);
		 queryWrapper.eq("wtlx","企业注册");
		 IPage<QcQywtfk> pageList = qcQywtfkService.page(page, queryWrapper);
		 return Result.OK(pageList);
	 }
	 /**
	  * 分页列表查询
	  *
	  * @param qcQywtfk
	  * @param pageNo
	  * @param pageSize
	  * @param req
	  * @return
	  */
	 //@AutoLog(value = "企业问题反馈-分页列表查询")
	 @ApiOperation(value="企业问题反馈-分页列表查询", notes="企业问题反馈-分页列表查询")
	 @GetMapping(value = "/list_qyzc4")
	 @PermissionData(pageComponent = "qcsb/qywtfk/QcQywtfkList_qyzc4")
	 public Result<IPage<QcQywtfk>> queryPageList_qyzc4(QcQywtfk qcQywtfk,
												   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
												   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
												   HttpServletRequest req) {
		 QueryWrapper<QcQywtfk> queryWrapper = QueryGenerator.initQueryWrapper(qcQywtfk, req.getParameterMap());
		 Page<QcQywtfk> page = new Page<QcQywtfk>(pageNo, pageSize);
		 LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		 String orgCode = sysUser.getOrgCode();
		 ISysBaseAPI api = (ISysBaseAPI) SpringContextUtils.getBean("sysBaseApiImpl");
		 List<String> rolesByUsername = api.getRolesByUsername(sysUser.getUsername());
		 for (String s : rolesByUsername) {
			 if (s.equals("sbyh")) {
				 queryWrapper.eq("create_by", sysUser.getUsername());
				 queryWrapper.eq("wtlx","企业注册");
				 queryWrapper.eq("wtzt", 4);
			 } else {
				 queryWrapper.eq("wtlx","企业注册");
				 queryWrapper.eq("wtzt", 4);
			 }
		 }
		 IPage<QcQywtfk> pageList = qcQywtfkService.page(page, queryWrapper);
		 return Result.OK(pageList);
	 }
	 //@AutoLog(value = "企业问题反馈-分页列表查询")
	 @ApiOperation(value="企业问题反馈-分页列表查询", notes="企业问题反馈-分页列表查询")
	 @GetMapping(value = "/list1")
	 public Result<IPage<QcQywtfk>> queryPageList1(QcQywtfk qcQywtfk,
												  @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
												  @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
												  HttpServletRequest req) {
		 QueryWrapper<QcQywtfk> queryWrapper = QueryGenerator.initQueryWrapper(qcQywtfk, req.getParameterMap());
		 Page<QcQywtfk> page = new Page<QcQywtfk>(pageNo, pageSize);
		 List<Integer> sbzts=new ArrayList<>();
		 sbzts.add(1);
		 sbzts.add(2);
		 queryWrapper.in("wtzt",sbzts);
		 queryWrapper.notIn("wtlx","企业注册");

		 IPage<QcQywtfk> pageList = qcQywtfkService.page(page, queryWrapper);
		 return Result.OK(pageList);
	 }
	 /**
	  * 分页列表查询
	  *
	  * @param qcQywtfk
	  * @param pageNo
	  * @param pageSize
	  * @param req
	  * @return
	  */
	 //@AutoLog(value = "企业问题反馈-分页列表查询")
	 @ApiOperation(value="企业问题反馈-分页列表查询", notes="企业问题反馈-分页列表查询")
	 @GetMapping(value = "/list2")
	 public Result<IPage<QcQywtfk>> queryPageList2(QcQywtfk qcQywtfk,
												  @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
												  @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
												  HttpServletRequest req) {
		 QueryWrapper<QcQywtfk> queryWrapper = QueryGenerator.initQueryWrapper(qcQywtfk, req.getParameterMap());
		 Page<QcQywtfk> page = new Page<QcQywtfk>(pageNo, pageSize);
		 List<Integer> sbzts=new ArrayList<>();
		 sbzts.add(3);
		 sbzts.add(4);
		 queryWrapper.in("wtzt",sbzts);
		 queryWrapper.notIn("wtlx","企业注册");
		 IPage<QcQywtfk> pageList = qcQywtfkService.page(page, queryWrapper);
		 return Result.OK(pageList);
	 }
	 /**
	  * 分页列表查询
	  *
	  * @param qcQywtfk
	  * @param pageNo
	  * @param pageSize
	  * @param req
	  * @return
	  */
	 //@AutoLog(value = "企业问题反馈-分页列表查询")
	 @ApiOperation(value="企业问题反馈-分页列表查询", notes="企业问题反馈-分页列表查询")
	 @GetMapping(value = "/list4")
	 @PermissionData(pageComponent = "qcsb/qywtfk/QcQywtfkList4")
	 public Result<IPage<QcQywtfk>> queryPageList4(QcQywtfk qcQywtfk,
												   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
												   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
												   HttpServletRequest req) {
		 QueryWrapper<QcQywtfk> queryWrapper = QueryGenerator.initQueryWrapper(qcQywtfk, req.getParameterMap());
		 Page<QcQywtfk> page = new Page<QcQywtfk>(pageNo, pageSize);
		 LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		 String orgCode = sysUser.getOrgCode();
		 ISysBaseAPI api = (ISysBaseAPI) SpringContextUtils.getBean("sysBaseApiImpl");
		 List<String> rolesByUsername = api.getRolesByUsername(sysUser.getUsername());
		 for (String s : rolesByUsername) {
			 if (s.equals("sbyh")) {
				 queryWrapper.eq("sys_org_code", orgCode);
				 queryWrapper.eq("wtzt", 4);
				 queryWrapper.notIn("wtlx","企业注册");
			 } else {
				 queryWrapper.eq("wtzt", 4);
				 queryWrapper.notIn("wtlx","企业注册");
			 }
		 }
		 IPage<QcQywtfk> pageList = qcQywtfkService.page(page, queryWrapper);
		 return Result.OK(pageList);
	 }
	 @AutoLog(value = "用地单位信息是否上报")
	 @ApiOperation(value="用地单位信息是否上报", notes="用地单位信息是否上报")
	 @GetMapping(value = "/sfsb")
	 public Result<String> sfshz(String id) {
		 qcQywtfkService.upSfsb(id);
		 return Result.OK("提交成功!");
	 }
	 @AutoLog(value = "用地单位信息是否审核")
	 @ApiOperation(value="用地单位信息是否审核", notes="用地单位信息是否审核")
	 @GetMapping(value = "/sfsh")
	 public Result<String> sfsh(String id) {
		 QcQywtfk qk=new QcQywtfk();
		 qk=qcQywtfkService.getById(id);
		 if(qk.getWtzt()==1) {
			 qk.setWtzt(2);
			 qcQywtfkService.saveOrUpdate(qk);
		 }
		 return Result.OK("审核中!");
	 }
	 @AutoLog(value = "用地单位信息是否可以撤回")
	 @ApiOperation(value="用地单位信息是否可以撤回", notes="用地单位信息是否可以撤回")
	 @GetMapping(value = "/sfch")
	 public Result<String> sfch(String id) {
		 QcQywtfk qc=new QcQywtfk();
		 qc=qcQywtfkService.getById(id);
		 if(qc.getWtzt()==1){
			 qc.setWtzt(0);
			 qcQywtfkService.saveOrUpdate(qc);
			 return Result.OK("撤回成功!");
		 }
		 return Result.error("无法撤回！");

	 }
	 @AutoLog(value = "用地单位信息审核结果")
	 @ApiOperation(value="用地单位信息审核结果", notes="用地单位信息审核结果")
	 @GetMapping(value = "/wcsh")
	 public Result<String> wcsh(String id) {
		 QcQywtfk qc=new QcQywtfk();
		 qc=qcQywtfkService.getById(id);
		 if(qc.getWtzt()!=null) {
			 if (qc.getSftgsh().equals("通过")) {
				 qc.setWtzt(3);
				 String org_code = qc.getSysOrgCode();
			 } else if (qc.getSftgsh().equals("不通过")) {
				 qc.setWtzt(4);
			 }
			 qcQywtfkService.saveOrUpdate(qc);
			 return Result.OK("完成审核!");
		 }else{
			 return Result.error("请先进行审核！");
		 }

	 }
	 @AutoLog(value = "用地单位信息重新上报")
	 @ApiOperation(value="用地单位信息重新上报", notes="用地单位信息重新上报")
	 @GetMapping(value = "/cxsb")
	 public Result<String> cxsb(String id) {
		 QcQywtfk qk=new QcQywtfk();
		 qk=qcQywtfkService.getById(id);
		 if(qk.getWtzt()==4) {
			 qk.setWtzt(0);
			 qcQywtfkService.saveOrUpdate(qk);
		 }
		 return Result.OK("审核中!");
	 }

	/**
	 *   添加
	 *
	 * @param qcQywtfk
	 * @return
	 */
	@AutoLog(value = "企业问题反馈-添加")
	@ApiOperation(value="企业问题反馈-添加", notes="企业问题反馈-添加")
	@PostMapping(value = "/add")
	public Result<String> add(@RequestBody QcQywtfk qcQywtfk) {
		qcQywtfkService.save(qcQywtfk);
		return Result.OK("添加成功！");
	}

	/**
	 *  编辑
	 *
	 * @param qcQywtfk
	 * @return
	 */
	@AutoLog(value = "企业问题反馈-编辑")
	@ApiOperation(value="企业问题反馈-编辑", notes="企业问题反馈-编辑")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> edit(@RequestBody QcQywtfk qcQywtfk) {
		qcQywtfkService.updateById(qcQywtfk);
		if(qcQywtfk.getWtzt()==1) {
			this.sfsh(qcQywtfk.getId());
		}
		return Result.OK("编辑成功!");
	}

	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "企业问题反馈-通过id删除")
	@ApiOperation(value="企业问题反馈-通过id删除", notes="企业问题反馈-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<String> delete(@RequestParam(name="id",required=true) String id) {
		qcQywtfkService.removeById(id);
		return Result.OK("删除成功!");
	}

	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "企业问题反馈-批量删除")
	@ApiOperation(value="企业问题反馈-批量删除", notes="企业问题反馈-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.qcQywtfkService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}

	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "企业问题反馈-通过id查询")
	@ApiOperation(value="企业问题反馈-通过id查询", notes="企业问题反馈-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<QcQywtfk> queryById(@RequestParam(name="id",required=true) String id) {
		QcQywtfk qcQywtfk = qcQywtfkService.getById(id);
		if(qcQywtfk==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(qcQywtfk);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param qcQywtfk
    */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, QcQywtfk qcQywtfk) {
        return super.exportXls(request, qcQywtfk, QcQywtfk.class, "企业问题反馈");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, QcQywtfk.class);
    }

}
