package cn.com.ctop.reimburse.mondule.controller;

import cn.com.ctop.common.module.service.ISysUserService;
import cn.com.ctop.common.module.utils.Check;
import cn.com.ctop.reimburse.mondule.entity.ReimburseExpress;
import cn.com.ctop.reimburse.mondule.service.IReimburseExpressService;
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.system.entity.SysUser;
import org.jeecg.common.system.query.QueryGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 报销申请——快递记录表
 *
 * @author jeecg-boot
 * @version V1.0
 * @date 2022-11-21
 */
@Slf4j
@Api(tags = "报销申请——快递记录表")
@RestController
@RequestMapping("/reimburse/express")
public class ReimburseExpressController {
    @Autowired
    private IReimburseExpressService reimburseExpressService;

    @Autowired
    private ISysUserService sysUserService;

    /**
     * 分页列表查询
     *
     * @param reimburseExpress
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @ApiOperation(value = "报销申请——快递记录表-分页列表查询", notes = "报销申请——快递记录表-分页列表查询")
    @GetMapping(value = "/list")
    public Result<IPage<ReimburseExpress>> queryPageList(ReimburseExpress reimburseExpress,
                                                         @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                         @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                         HttpServletRequest req) {
        Result<IPage<ReimburseExpress>> result = new Result<>();
        String statDate = reimburseExpress.getStatDate();
        String userName = reimburseExpress.getUserName();
        reimburseExpress = new ReimburseExpress();
        QueryWrapper<ReimburseExpress> queryWrapper = QueryGenerator.initQueryWrapper(reimburseExpress, req.getParameterMap());
        if (!Check.isNull(statDate)) {
            queryWrapper.likeRight("stat_date", statDate);
        }
        if (!Check.isNull(userName)) {
            queryWrapper.like("user_name", userName);
        }
        queryWrapper.orderByDesc("create_time");
        Page<ReimburseExpress> page = new Page<ReimburseExpress>(pageNo, pageSize);
        IPage<ReimburseExpress> pageList = reimburseExpressService.page(page, queryWrapper);
        result.setSuccess(true);
        result.setResult(pageList);
        return result;
    }

    /**
     * 添加
     */
    @PostMapping(value = "/add")
    public Result<ReimburseExpress> add(@RequestBody ReimburseExpress reimburseExpress) {
        Result<ReimburseExpress> result = new Result<>();
        try {
            SysUser user = sysUserService.getById(reimburseExpress.getUserId());
            if (!Check.isNull(user)) {
                reimburseExpress.setUserName(user.getRealname());
            }
            reimburseExpressService.save(reimburseExpress);
            result.success("添加成功！");
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            result.error500("操作失败");
        }
        return result;
    }

    /**
     * 导入EXCEL批量创建
     */
    @PostMapping(value = "/importExcel")
    @ResponseBody
    public Result<Object> importExcel(HttpServletResponse response, @RequestParam("file") MultipartFile file) {
        response.setHeader("Access-Control-Allow-Origin", "*");
        try {
            if (Check.isNull(file)) {
                return Result.error("执行失败，请上传应用Excel文件");
            }
            return reimburseExpressService.importExcel(file);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("导入EXCEL失败，" + e.getMessage());
        }
    }

    /**
     * 编辑
     *
     * @param reimburseExpress
     * @return
     */
    @ApiOperation(value = "报销申请——快递记录表-编辑", notes = "报销申请——快递记录表-编辑")
    @PostMapping(value = "/edit")
    public Result<ReimburseExpress> edit(@RequestBody ReimburseExpress reimburseExpress) {
        Result<ReimburseExpress> result = new Result<ReimburseExpress>();
        ReimburseExpress reimburseExpressEntity = reimburseExpressService.getById(reimburseExpress.getId());
        if (reimburseExpressEntity == null) {
            result.error500("未找到对应实体");
        } else {
            boolean ok = reimburseExpressService.updateById(reimburseExpress);
            if (ok) {
                result.success("修改成功!");
            }
        }

        return result;
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "报销申请——快递记录表-通过id删除", notes = "报销申请——快递记录表-通过id删除")
    @GetMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name = "id") String id) {
        try {
            reimburseExpressService.removeById(id);
        } catch (Exception e) {
            log.error("删除失败", e.getMessage());
            return Result.error("删除失败!");
        }
        return Result.ok("删除成功!");
    }


    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "报销申请——快递记录表-通过id查询", notes = "报销申请——快递记录表-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<ReimburseExpress> queryById(@RequestParam(name = "id", required = true) String id) {
        Result<ReimburseExpress> result = new Result<>();
        ReimburseExpress reimburseExpress = reimburseExpressService.getById(id);
        if (reimburseExpress == null) {
            result.error500("未找到对应实体");
        } else {
            result.setResult(reimburseExpress);
            result.setSuccess(true);
        }
        return result;
    }
}
