package com.hzs.system.config.controller.manage;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hzs.common.core.enums.EApprovalBusiness;
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.serviceCharge.BdServiceCharge;
import com.hzs.common.domain.serviceCharge.BdServiceChargeDetail;
import com.hzs.common.security.utils.SecurityUtils;
import com.hzs.system.config.service.IBdServiceChargeDetailService;
import com.hzs.system.config.service.IBdServiceChargeService;

import com.hzs.system.config.vo.ServiceChargeVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description:
 * @Author: yuhui
 * @Time: 2023/1/3 11:48
 * @Classname: BdServiceChargeController
 * @PackageName: com.hzs.sale.serviceCharge.controller.manage
 */
@Slf4j
@RestController
@RequestMapping("/manage/service-charge")
public class BdServiceChargeController extends BaseController {

    @Autowired
    private IBdServiceChargeService serviceChargeService;
    @Autowired
    private IBdServiceChargeDetailService serviceChargeDetailService;
    /**
     * 查询配置列表
     * @param serviceCharge
     * @return
     */
    @GetMapping("list")
    public TableDataInfo list(BdServiceCharge serviceCharge){

        startPage();
        serviceCharge.setPkCountry(SecurityUtils.getPkCountry());
        List<ServiceChargeVo> list = serviceChargeService.selectByList(serviceCharge);
        List<ServiceChargeVo> resultList = list.stream().map(obj -> {
            ServiceChargeVo serviceChargeVo = BeanUtil.copyProperties(obj, ServiceChargeVo.class);
            EApprovalBusiness approvalBusiness = EApprovalBusiness.getEnumByValue(obj.getType());
            if (approvalBusiness != null){
                serviceChargeVo.setTypeName(approvalBusiness.getLabel());
            }
            serviceChargeVo.setPkServiceCharge(obj.getPkId());
            QueryWrapper<BdServiceChargeDetail> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("PK_SERVICE_CHARGE", obj.getPkId());
            List<BdServiceChargeDetail> serviceChargeDetailList = serviceChargeDetailService.list(queryWrapper);
            serviceChargeVo.setServiceChargeDetailsList(serviceChargeDetailList);
            return serviceChargeVo;
        }).collect(Collectors.toList());
        return getDataTable(resultList);
    }

    /**
     * 新增
     * @param serviceChargeVo
     * @return
     */
    @PostMapping("save")
    public AjaxResult save(@RequestBody ServiceChargeVo serviceChargeVo){
        // bd_account  主键
        Integer pkCountry= SecurityUtils.getPkCountry();
        QueryWrapper<BdServiceCharge> qw = new QueryWrapper<>();
        qw.eq("TYPE",serviceChargeVo.getType());
        qw.eq("PK_COUNTRY", pkCountry);
        int count = serviceChargeService.count(qw);
        if (count > 0){
            return AjaxResult.error("类型已存在,不能重复添加");
        }
        serviceChargeVo.setPkCountry(pkCountry);
        serviceChargeService.saveServiceCharge(serviceChargeVo);
        return AjaxResult.success();
    }

    /**
     * 修改
     * @param serviceChargeVo
     * @return
     */
    @PostMapping("update")
    public AjaxResult update(@RequestBody ServiceChargeVo serviceChargeVo){
        serviceChargeService.updateServiceCharge(serviceChargeVo);
        return AjaxResult.success();
    }

    /**
     * 删除
     * @param pkServiceCharge
     * @return
     */
    @GetMapping("delete/{pkServiceCharge}")
    public AjaxResult delete(@PathVariable Long pkServiceCharge){
        serviceChargeService.deleteServiceCharge(pkServiceCharge);
        return AjaxResult.success();
    }

    /**
     * 查询详情
     * @return
     */
    @GetMapping("detail/{pkServiceCharge}")
    public AjaxResult detail(@PathVariable Long pkServiceCharge){
        return AjaxResult.success(serviceChargeService.getServiceCharge(pkServiceCharge));
    }
}
