package com.ozs.web.controller.plan;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ozs.base.domain.BaseNoticeType;
import com.ozs.common.annotation.Log;
import com.ozs.common.constant.Constants;
import com.ozs.common.constant.ModularConstans;
import com.ozs.common.core.controller.BaseController;
import com.ozs.common.core.domain.AjaxResult;
import com.ozs.common.core.domain.entity.SysDictData;
import com.ozs.common.enums.BusinessType;
import com.ozs.common.enums.SysFileRefEnum;
import com.ozs.plan.doman.MonthlyReconciliation;
import com.ozs.plan.doman.vo.requestVo.MonthlyReconciliationPageReqVo;
import com.ozs.plan.service.MonthlyReconciliationService;
import com.ozs.system.domain.SysFileInfo;
import com.ozs.system.domain.SysFileRef;
import com.ozs.system.domain.vo.responseVo.SysDeptResponseVo;
import com.ozs.system.service.ISysDeptService;
import com.ozs.system.service.ISysDictTypeService;
import com.ozs.system.service.SysFileRefService;
import com.ozs.system.service.SysFileService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotEmpty;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@RestController
@RequestMapping("/monthly/reconciliation")
@Api(tags = ModularConstans.monthly)
public class MonthlyReconciliationController extends BaseController {


    @Autowired
    private MonthlyReconciliationService monthlyReconciliationService;
    @Autowired
    private SysFileRefService sysFileRefService;
    @Autowired
    private SysFileService sysFileService;
    @Autowired
    private ISysDeptService iSysDeptService;
    @Autowired
    private ISysDictTypeService dictTypeService;

    @ApiOperation(value = "分页查询月度对表")
    @PostMapping("/page")
    @PreAuthorize("@ss.hasPermi('base:monthly:list')")
    @Log(title = ModularConstans.monthly, businessType = BusinessType.QUERY)
    public AjaxResult page(@NotEmpty(message = "数据为空")
                           @RequestBody MonthlyReconciliationPageReqVo vo) {
        LambdaQueryWrapper<MonthlyReconciliation> lw = new LambdaQueryWrapper<>();
        if (!StringUtils.isBlank(vo.getProjectName())) {
            lw.like(MonthlyReconciliation::getProjectName, vo.getProjectName());
        }
        if (!ObjectUtils.isEmpty(vo.getProjectStatus())) {
            lw.eq(MonthlyReconciliation::getProjectStatus, vo.getProjectStatus());
        }
        if (!StringUtils.isBlank(vo.getPurchaseServices())) {
            lw.eq(MonthlyReconciliation::getPurchaseServices, vo.getPurchaseServices());
        }
        if(!ObjectUtils.isEmpty(vo.getStartTime())){
            lw.ge(MonthlyReconciliation::getPlanDemandSubTime,vo.getStartTime());
        }
        if(!ObjectUtils.isEmpty(vo.getStartTime())){
            lw.le(MonthlyReconciliation::getPlanDemandSubTime,vo.getEntTime());
        }
        IPage<MonthlyReconciliation> page = monthlyReconciliationService.page(new Page<>(vo.getPageNum(), vo.getPageSize()), lw);
        if (!ObjectUtils.isEmpty(page.getRecords())) {
            page.setRecords(page.getRecords().stream().map(dto -> {
                // 采购单位名称
                Map<String, Object> stringObjectMap = iSysDeptService.selectDeptById(dto.getPurchaseDeptId());
                if (!ObjectUtils.isEmpty(stringObjectMap)) {
                    SysDeptResponseVo sysDept = (SysDeptResponseVo) stringObjectMap.get("sysDept");
                    dto.setPurchaseDeptName(sysDept.getDeptName());
                }
                // 采购服务站名称
                List<SysDictData> data = dictTypeService.selectDictDataByType(Constants.PURCHASE_SERVICES);
                if (!ObjectUtils.isEmpty(data)) {
                    List<SysDictData> collect = data.stream()
                            .filter(d -> d.getDictValue().equals(dto.getPurchaseServices())).collect(Collectors.toList());
                    if(!ObjectUtils.isEmpty(collect)){
                        dto.setPurchaseServicesName(collect.get(0).getDictLabel());
                    }
                }
                return dto;
            }).collect(Collectors.toList()));
        }
        return success(page);

    }


    @ApiOperation(value = "查看月度对表详情")
    @PostMapping("/getInfo")
    @PreAuthorize("@ss.hasPermi('base:monthly:query')")
    @Log(title = ModularConstans.monthly, businessType = BusinessType.QUERY)
    public AjaxResult getInfo(@NotEmpty(message = "主键id不能为空")
                              @RequestParam(value = "id", required = true)
                                      Long id) {
        MonthlyReconciliation vo = monthlyReconciliationService.getById(id);

        if (!ObjectUtils.isEmpty(vo)) {
            LambdaQueryWrapper<SysFileRef> lw = new LambdaQueryWrapper();
            lw.eq(SysFileRef::getType, SysFileRefEnum.PLAN_TEMPORARY.getType());
            lw.eq(SysFileRef::getRedId, vo.getPlanId());
            List<SysFileRef> list = sysFileRefService.list(lw);
            if (!ObjectUtils.isEmpty(list)) {
                LambdaQueryWrapper<SysFileInfo> l = new LambdaQueryWrapper();
                l.in(SysFileInfo::getFileId, list.stream().map(SysFileRef::getFileId).collect(Collectors.toList()));
                vo.setFileList(sysFileService.list(l));
            }
            Map<String, Object> stringObjectMap = iSysDeptService.selectDeptById(vo.getPurchaseDeptId());
            if (!ObjectUtils.isEmpty(stringObjectMap)) {
                SysDeptResponseVo sysDept = (SysDeptResponseVo) stringObjectMap.get("sysDept");
                vo.setPurchaseDeptName(sysDept.getDeptName());
            }

            // 采购服务站名称
            List<SysDictData> data = dictTypeService.selectDictDataByType("purchase_services");
            if (!ObjectUtils.isEmpty(data)) {
                List<SysDictData> collect = data.stream()
                        .filter(d -> d.getDictValue().equals(vo.getPurchaseServices())).collect(Collectors.toList());
                if(!ObjectUtils.isEmpty(collect)){
                    vo.setPurchaseServicesName(collect.get(0).getDictLabel());
                }
            }
        }

        return success(vo);
    }

}
