# RevenueMarketCodeController.java

    package cloud.wconnect.rateshop.controller.revenue;

    import cloud.wconnect.commonlib.annotation.ApiId;
    import cloud.wconnect.commonlib.component.BeanMapper;
    import cloud.wconnect.commonlib.component.SessionContext;
    import cloud.wconnect.commonlib.cons.BaseErrorCodeEnum;
    import cloud.wconnect.commonlib.exception.ForbiddenException;
    import cloud.wconnect.rateshop.entity.revenue.RevenueMarketCode;
    import cloud.wconnect.rateshop.pojo.dto.revenue.MarketCodeDTO;
    import cloud.wconnect.rateshop.service.revenue.IRevenueMarketCodeService;
    import jakarta.validation.Valid;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.*;

    import java.util.Collections;
    import java.util.Comparator;
    import java.util.List;
    import java.util.Map;

    /**
     * <p>
     *  前端控制器
     * </p>
     *
     * @author yyt
     * @since 2024-03-23
     */
    @RestController
    @RequestMapping("/revenue/market-code")
    public class RevenueMarketCodeController {

        @Autowired
        private SessionContext sessionContext;
        @Autowired
        private IRevenueMarketCodeService revenueMarketCodeService;
        @Autowired
        private BeanMapper beanMapper;

        /**
         * 1109 - 获取 market code
         *
         * @param property 资产标识
         * @return 获取 market code
         * @author yyt
         * @date 2024/3/23
         **/
        @ApiId(1109)
        @GetMapping
        public List<MarketCodeDTO> list(String property) {
            List<RevenueMarketCode> marketCodes = revenueMarketCodeService.listEffiective(sessionContext.getPropertyId(property));
            marketCodes.sort(Comparator.comparing(RevenueMarketCode::getId).reversed());
            return beanMapper.mapList(marketCodes, MarketCodeDTO.class);
        }

        /**
         * 1110 - 新增、编辑 market code
         *
         * @param id            market code id
         * @param property      资产标识
         * @param marketCodeDTO market code
         * @return {}
         * @author yyt
         * @date 2024/3/23
         **/
        @ApiId(1110)
        @PostMapping("/{id:\\d+}")
        public Map save(@PathVariable int id, String property, @Valid @RequestBody MarketCodeDTO marketCodeDTO){
            int propertyId = sessionContext.getPropertyId(property);
            int loginUserId = sessionContext.getLoginUserId();

            if (id == 0) {
                revenueMarketCodeService.checkRepeat(propertyId, marketCodeDTO);

                revenueMarketCodeService.save(propertyId, marketCodeDTO, loginUserId);
            } else {
                marketCodeDTO.setId(id);
                revenueMarketCodeService.checkRepeat(propertyId, marketCodeDTO);

                RevenueMarketCode revenueMarketCode = revenueMarketCodeService.checkExist(id);
                checkBelong(revenueMarketCode, propertyId);

                revenueMarketCodeService.update(revenueMarketCode, marketCodeDTO, loginUserId);
            }

            return Collections.emptyMap();
        }

        /**
         * 1111 - 删除 market code
         *
         * @param id       market code id
         * @param property 资产标识
         * @return {}
         * @author yyt
         * @date 2024/3/23
         **/
        @ApiId(1111)
        @DeleteMapping("/{id:\\d+}")
        public Map delete(@PathVariable int id, String property) {
            int propertyId = sessionContext.getPropertyId(property);
            int loginUserId = sessionContext.getLoginUserId();
            RevenueMarketCode revenueMarketCode = revenueMarketCodeService.checkExist(id);
            checkBelong(revenueMarketCode, propertyId);

            revenueMarketCodeService.delete(revenueMarketCode, loginUserId);

            return Collections.emptyMap();
        }

        /**
         * 校验数据归属
         *
         * @param revenueMarketCode market code
         * @param paramPropertyId   资产
         * @author yyt
         * @date 2024/3/23
         */
        private void checkBelong(RevenueMarketCode revenueMarketCode, Integer paramPropertyId) {
            if (!revenueMarketCode.getPropertyId().equals(paramPropertyId)) {
                throw new ForbiddenException(BaseErrorCodeEnum.DATA_FORBIDDEN.getI18nCode(),
                        BaseErrorCodeEnum.DATA_FORBIDDEN.getCode(), "the property id parameter is different from " +
                        "the property id of the revenueMarketCode;propertyId:" + paramPropertyId +
                        ",revenueMarketCodePropertyId:" + revenueMarketCode.getPropertyId() +
                        ",revenueMarketCodeId:" + revenueMarketCode.getId());
            }
        }

    }