package com.hzs.sale.modifyrecord.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hzs.common.core.domain.R;
import com.hzs.common.core.enums.EYesNo;
import com.hzs.common.core.web.controller.BaseController;
import com.hzs.common.core.web.domain.AjaxResult;
import com.hzs.common.core.web.page.TableDataInfo;
import com.hzs.common.domain.member.base.CuMember;
import com.hzs.common.domain.sale.modifyRecord.BdModifyRecord;
import com.hzs.common.security.service.UserTokenService;
import com.hzs.member.base.IMemberServiceApi;
import com.hzs.sale.modifyrecord.service.IBdModifyRecordService;
import com.hzs.sale.wares.vo.ModifyRecordVo;
import com.hzs.system.sys.dto.LoginUser;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description:
 * @Author: yuhui
 * @Time: 2023/2/28 19:03
 * @Classname: ModifyRecordController
 * @PackageName: com.hzs.sale.modifyRecord.controller
 */
@RestController
@RequestMapping("/manage/modify-record")
public class ModifyRecordController extends BaseController {

    @Autowired
    private IBdModifyRecordService modifyRecordService;


    @Autowired
    private UserTokenService userTokenService;
    /**
     * 查询商品审核列表
     * @return
     */
    @GetMapping("list")
    public TableDataInfo list(ModifyRecordVo mr){
        startPage();

        QueryWrapper<BdModifyRecord> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(mr.getName())){
            queryWrapper.eq("NAME",mr.getName());
        }
        if (StringUtils.isNotBlank(mr.getCode())){
            queryWrapper.eq("CODE",mr.getCode());
        }
        // 发起部门
        if (mr.getPkCreationDept() != null){
            queryWrapper.eq("PK_CREATION_DEPT",mr.getPkCreationDept());
        }
        // f发起人
        if (mr.getCreatorName() != null){
            queryWrapper.eq("CREATOR_NAME",mr.getCreatorName());
        }
        // 审核部门
        if (mr.getPkReviewDept() != null){
            queryWrapper.eq("PK_REVIEW_DEPT",mr.getPkReviewDept());
        }
        // 审核人
        if (mr.getReviewName() != null){
            queryWrapper.eq("REVIEW_NAME",mr.getReviewName());
        }
        // 审核状态
        if (mr.getAuditStatus() != null){
            queryWrapper.eq("AUDIT_STATUS",mr.getAuditStatus());
        }
        // 发起时间区间
        if (mr.getStartCreationTime() != null && mr.getEndCreationTime() != null){
            queryWrapper.between("CREATION_TIME",mr.getStartCreationTime(),mr.getEndCreationTime());
        }
        // 审核时间区间
        if (mr.getStartReviewTime() != null && mr.getEndReviewTime() != null){
            queryWrapper.between("REVIEW_TIME",mr.getStartReviewTime(),mr.getEndReviewTime());
        }
        List<Integer> typeList = new ArrayList<>();
        if (mr.getType() == EYesNo.YES.getIntValue()){
            // 查询商品
            typeList.add(1);
            typeList.add(2);
        }else if (mr.getType() == EYesNo.NO.getIntValue()){
            // 查询产品
            typeList.add(3);
            typeList.add(4);
        }
        List<BdModifyRecord> list = modifyRecordService.list();
        List<ModifyRecordVo> resultList = new ArrayList<>();
        for (BdModifyRecord modifyRecord : list) {
            ModifyRecordVo modifyRecordVo = BeanUtil.copyProperties(modifyRecord, ModifyRecordVo.class);
            modifyRecordVo.setContent(modifyRecord.getNewValue());
            resultList.add(modifyRecordVo);
        }
        return getDataTable(resultList);
    }

    /**
     * 商品审批
     * @return
     */
    @PostMapping("examine")
    public AjaxResult examine(@RequestBody BdModifyRecord modifyRecord){
        LoginUser loginUser = userTokenService.getLoginUser();
        modifyRecordService.examineAndVerify(modifyRecord,loginUser);
        return AjaxResult.success();
    }
}
