package org.jeecg.modules.jixiaoPersonPrice.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.jixiaoPersonPrice.convert.JixiaoPersonPriceConvert;
import org.jeecg.modules.jixiaoPersonPrice.dto.JixiaoPersonPriceAddDTO;
import org.jeecg.modules.jixiaoPersonPrice.dto.JixiaoPersonPriceExportDTO;
import org.jeecg.modules.jixiaoPersonPrice.entity.JixiaoPersonPrice;
import org.jeecg.modules.jixiaoPersonPrice.service.IJixiaoPersonPriceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @Description: 人员绩效提成
 * @Author: jeecg-boot
 * @Date:   2023-09-04
 * @Version: V1.0
 */
@Api(tags="..人员绩效提成")
@RestController
@RequestMapping("/jixiaoPersonPrice/jixiaoPersonPrice")
@Slf4j
public class JixiaoPersonPriceController extends JeecgController<JixiaoPersonPrice, IJixiaoPersonPriceService> {
	@Autowired
	private IJixiaoPersonPriceService jixiaoPersonPriceService;

	/**查询导出的人员绩效*/
	@ApiOperation("查询导出的人员绩效——查询数据")
	@GetMapping(value = "/exportPersonJX")
	public List<Map<String,String>> exportPersonJX(JixiaoPersonPriceExportDTO dto){
		return jixiaoPersonPriceService.exportPersonJX(dto);
	}

	/**
	 * 人员绩效——按时间导出excel
	 */
	@ApiOperation("人员绩效——按时间导出excel")
	@RequestMapping(value = "/exportXlsByTime")
	public ModelAndView exportXlsByTime(HttpServletResponse response, String beginDate,String endDate) {
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
		Date date= null;
		try {
			date = sdf.parse(endDate);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		endDate=sdf.format(date.getTime() + (1000 * 60 * 60 * 24));

		JixiaoPersonPriceExportDTO dto = new JixiaoPersonPriceExportDTO();
		dto.setBeginDate(beginDate);
		dto.setEndDate(endDate);
		List<String> personPrices = jixiaoPersonPriceService.selectPersonByDate(dto);
		if(personPrices!=null && !personPrices.isEmpty()){
			List<Map<String,String>> list = jixiaoPersonPriceService.exportPersonJX(dto);
			return jixiaoPersonPriceService.exportXlsByTime(response, list, "人员绩效提成");
		}else{
			List<Map<String,String>> list = new ArrayList<>();
			return jixiaoPersonPriceService.exportXlsByTime(response, list, "人员绩效提成");
		}
	}

	 /**
	  * 人员绩效——批量新增
	  */
	 @AutoLog(value = "人员绩效——批量新增")
	 @ApiOperation(value="人员绩效——批量新增", notes="人员绩效——批量新增")
	 @PostMapping(value = "/addAllJX")
	 public Result<String> addAllJX(@RequestBody List<JixiaoPersonPriceAddDTO> list) {
	 	List<JixiaoPersonPrice> list1 = list.stream().map(i-> JixiaoPersonPriceConvert.INSTANCE.convert(i)).collect(Collectors.toList());
	 	jixiaoPersonPriceService.saveBatch(list1);
	 	return Result.OK("添加成功！");
	 }

	/**
	 * 分页列表查询
	 *
	 * @param jixiaoPersonPrice
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	//@AutoLog(value = "人员绩效提成-分页列表查询")
	@ApiOperation(value="人员绩效提成-分页列表查询", notes="人员绩效提成-分页列表查询")
	@GetMapping(value = "/list")
	public Result<IPage<JixiaoPersonPrice>> queryPageList(JixiaoPersonPrice jixiaoPersonPrice,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<JixiaoPersonPrice> queryWrapper = QueryGenerator.initQueryWrapper(jixiaoPersonPrice, req.getParameterMap());
		Page<JixiaoPersonPrice> page = new Page<JixiaoPersonPrice>(pageNo, pageSize);
		IPage<JixiaoPersonPrice> pageList = jixiaoPersonPriceService.page(page, queryWrapper);
		return Result.OK(pageList);
	}

	/**
	 *   添加
	 *
	 * @param jixiaoPersonPrice
	 * @return
	 */
	@AutoLog(value = "人员绩效提成-添加")
	@ApiOperation(value="人员绩效提成-添加", notes="人员绩效提成-添加")
	//@RequiresPermissions("org.jeecg.modules:itdm_jixiao_person_price:add")
	@PostMapping(value = "/add")
	public Result<String> add(@RequestBody JixiaoPersonPrice jixiaoPersonPrice) {
		jixiaoPersonPriceService.save(jixiaoPersonPrice);
		return Result.OK("添加成功！");
	}

	/**
	 *  编辑
	 *
	 * @param jixiaoPersonPrice
	 * @return
	 */
	@AutoLog(value = "人员绩效提成-编辑")
	@ApiOperation(value="人员绩效提成-编辑", notes="人员绩效提成-编辑")
	//@RequiresPermissions("org.jeecg.modules:itdm_jixiao_person_price:edit")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> edit(@RequestBody JixiaoPersonPrice jixiaoPersonPrice) {
		jixiaoPersonPriceService.updateById(jixiaoPersonPrice);
		return Result.OK("编辑成功!");
	}

	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "人员绩效提成-通过id删除")
	@ApiOperation(value="人员绩效提成-通过id删除", notes="人员绩效提成-通过id删除")
	//@RequiresPermissions("org.jeecg.modules:itdm_jixiao_person_price:delete")
	@DeleteMapping(value = "/delete")
	public Result<String> delete(@RequestParam(name="id",required=true) String id) {
		jixiaoPersonPriceService.removeById(id);
		return Result.OK("删除成功!");
	}

	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "人员绩效提成-批量删除")
	@ApiOperation(value="人员绩效提成-批量删除", notes="人员绩效提成-批量删除")
	//@RequiresPermissions("org.jeecg.modules:itdm_jixiao_person_price:deleteBatch")
	@DeleteMapping(value = "/deleteBatch")
	public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.jixiaoPersonPriceService.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<JixiaoPersonPrice> queryById(@RequestParam(name="id",required=true) String id) {
		JixiaoPersonPrice jixiaoPersonPrice = jixiaoPersonPriceService.getById(id);
		if(jixiaoPersonPrice==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(jixiaoPersonPrice);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param jixiaoPersonPrice
    */
    //@RequiresPermissions("org.jeecg.modules:itdm_jixiao_person_price:exportXls")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, JixiaoPersonPrice jixiaoPersonPrice) {
        return super.exportXls(request, jixiaoPersonPrice, JixiaoPersonPrice.class, "人员绩效提成");
    }

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

}
