package com.zcxk.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zcxk.common.EasyExcelUtil;
import com.zcxk.dto.QueryCondition;
import com.zcxk.entity.UserRegister;
import com.zcxk.model.AjaxMessage;
import com.zcxk.model.Pagination;
import com.zcxk.model.ResultStatus;
import com.zcxk.service.UserRegisterService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;

/**
 * 用户注册(UserRegister)控制层
 *
 * @author yjy
 * @since 2020-10-29
 */
@RestController
@RequestMapping("/userRegister")
@Api(tags = "注册申请")
public class UserRegisterController {
    /**
     * 服务对象
     */
    @Autowired
    private UserRegisterService userRegisterService;
    @Value("${UMIS.sys_excel_path}")
    private String baseDir;
    /**
     * 新增一条数据
     *
     * @param userRegister 实体类
     * @return Response对象
     */
    @RequestMapping(value = "insert", method = RequestMethod.POST)
    @ApiOperation(value = "插入注册申请")
    public AjaxMessage<Integer> insert(@ApiParam(value = "注册申请信息", required = true) @RequestBody UserRegister userRegister) {
        int result = userRegisterService.insert(userRegister);

        return new AjaxMessage<>(ResultStatus.OK, result);
    }

    /**
     * 删除多条数据
     *
     * @param ids 主键，以逗号隔开
     * @return Response对象
     */
    @RequestMapping(value = "delete", method = RequestMethod.POST)
    @ApiOperation(value = "删除注册申请")
    public AjaxMessage<Integer> delete(@ApiParam(value = "注册ids", required = true) @RequestBody Long[] ids) {
        int result = userRegisterService.deleteById(ids);
        return new AjaxMessage<>(ResultStatus.OK, result);
    }


    /**
     * 分页查询
     *
     * @return Response对象
     */
    @RequestMapping(value = "selectPage", method = RequestMethod.POST)
    @ApiOperation(value = "查询用户注册申请列表")
    public AjaxMessage<Pagination<UserRegister>> selectPage(@ApiParam(value = "查询条件", required = true) @RequestBody QueryCondition queryCondition) throws ParseException {
        UserRegister userRegister = new UserRegister();
        userRegister.setUsername(queryCondition.getKeywords());
        SimpleDateFormat sdf_day = new SimpleDateFormat("yyyy-MM-dd");
        if(StringUtils.isNotBlank(queryCondition.getBeginTime())){
            userRegister.setDateCreate(sdf_day.parse(queryCondition.getBeginTime()));
        }
        if(StringUtils.isNotBlank(queryCondition.getEndTime())){
            userRegister.setDateUpdate(sdf_day.parse(queryCondition.getEndTime()));
        }
        IPage<UserRegister> iPage = new Page<>(queryCondition.getPageNum(), queryCondition.getPageSize());
        iPage = userRegisterService.selectPage (userRegister, iPage);
        Pagination<UserRegister> pages = new Pagination<>(iPage);
        return new AjaxMessage<>(ResultStatus.OK, pages);
    }

   @RequestMapping(value = "/exportExcel", method = RequestMethod.POST)
    @ApiOperation(value = "注册申请列表导出")
    public AjaxMessage<String> exportExcel(@ApiParam(value = "查询条件", required = true) @RequestBody QueryCondition queryCondition) {
        List<UserRegister> result = userRegisterService.selectListByCondition(queryCondition);
        String filePath = EasyExcelUtil.excelWrite(baseDir, UserRegister.class, "用户注册申请记录", result);
        return new AjaxMessage<>(ResultStatus.OK, filePath);
    }

}