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

import cn.com.ctop.common.module.utils.Check;
import cn.com.ctop.reimburse.mondule.entity.ReimburseProject;
import cn.com.ctop.reimburse.mondule.service.IReimburseProjectService;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
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 lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
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.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * 报销申请项目表
 *
 * @author jeecg-boot
 * @version V1.0
 * @date 2022-11-21
 */
@Slf4j
@Api(tags = "报销申请项目表")
@RestController
@RequestMapping("/reimburse/project")
public class ReimburseProjectController {
    @Autowired
    private IReimburseProjectService reimburseProjectService;

    /**
     * 分页列表查询
     */
    @GetMapping(value = "/list")
    public Result<IPage<ReimburseProject>> queryPageList(ReimburseProject reimburseProject,
                                                         @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                         @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                         HttpServletRequest req) {
        Result<IPage<ReimburseProject>> result = new Result<>();
        QueryWrapper<ReimburseProject> queryWrapper = QueryGenerator.initQueryWrapper(reimburseProject, req.getParameterMap());
        Page<ReimburseProject> page = new Page<ReimburseProject>(pageNo, pageSize);
        IPage<ReimburseProject> pageList = reimburseProjectService.page(page, queryWrapper);
        result.setSuccess(true);
        result.setResult(pageList);
        return result;
    }

    /**
     * 添加
     */
    @PostMapping(value = "/add")
    public Result<Object> add(@RequestBody ReimburseProject reimburseProject) {
        try {
            reimburseProjectService.save(reimburseProject);
            return Result.ok(reimburseProject.getId());
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return Result.error("操作失败");
        }
    }

    /**
     * 编辑
     */
    @PostMapping(value = "/edit")
    public Result<ReimburseProject> edit(@RequestBody ReimburseProject reimburseProject) {
        Result<ReimburseProject> result = new Result<ReimburseProject>();
        ReimburseProject reimburseProjectEntity = reimburseProjectService.getById(reimburseProject.getId());
        if (reimburseProjectEntity == null) {
            result.error500("未找到对应实体");
        } else {
            boolean ok = reimburseProjectService.updateById(reimburseProject);
            if (ok) {
                result.success("修改成功!");
            }
        }
        return result;
    }

    /**
     * 集体修改状态
     */
    @PostMapping(value = "/editMoreStatus")
    public Result<Object> editMoreStatus(@RequestBody JSONObject data) {
        List<Long> ids = JSONArray.parseArray(data.getJSONArray("ids").toJSONString(), Long.class);
        String status = data.getString("status");
        List<ReimburseProject> list = new ArrayList<>();
        for (Long id : ids) {
            ReimburseProject p = new ReimburseProject();
            p.setId(id);
            p.setStatus(status);
            list.add(p);
        }
        reimburseProjectService.updateBatchById(list);
        return Result.ok("success");
    }

    /**
     * 通过id删除
     */
    @GetMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name = "id") String id) {
        try {
            reimburseProjectService.removeById(id);
        } catch (Exception e) {
            log.error("删除失败", e.getMessage());
            return Result.error("删除失败!");
        }
        return Result.ok("删除成功!");
    }

    /**
     * 批量删除
     */
    @GetMapping(value = "/deleteBatch")
    public Result<ReimburseProject> deleteBatch(@RequestParam(name = "ids") String ids) {
        Result<ReimburseProject> result = new Result<>();
        if (ids == null || "".equals(ids.trim())) {
            result.error500("参数不识别！");
        } else {
            this.reimburseProjectService.removeByIds(Arrays.asList(ids.split(",")));
            result.success("删除成功!");
        }
        return result;
    }

    /**
     * 通过审核下的所有附件
     */
    @GetMapping(value = "/queryByApplyId")
    public Result<Object> queryByApplyId(Long applyId) {
        QueryWrapper<ReimburseProject> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("apply_id", applyId);
        List<ReimburseProject> list = reimburseProjectService.list(queryWrapper);
        Set<Object> urls = new HashSet<>();
        for (ReimburseProject project : list) {
            if (!Check.isNull(project.getFileUrls())) {
                List<String> fileUrls = JSONArray.parseArray(project.getFileUrls().toString(), String.class);
                if (!Check.isNull(fileUrls)) {
                    urls.addAll(fileUrls);
                }
            }
        }
        return Result.ok(urls);
    }

    /**
     * 通过id查询
     */
    @GetMapping(value = "/queryById")
    public Result<ReimburseProject> queryById(@RequestParam(name = "id", required = true) String id) {
        Result<ReimburseProject> result = new Result<>();
        ReimburseProject reimburseProject = reimburseProjectService.getById(id);
        if (reimburseProject == null) {
            result.error500("未找到对应实体");
        } else {
            result.setResult(reimburseProject);
            result.setSuccess(true);
        }
        return result;
    }

    /**
     * 通过id查询
     */
    @PostMapping(value = "/queryByIds")
    public Result<Object> queryByIds(@RequestBody JSONArray data) {
        List<String> ids = JSONObject.parseArray(data.toJSONString(), String.class);
        List<ReimburseProject> list = reimburseProjectService.getByIds(ids);
        return Result.ok(list);
    }

}
