package com.hzs.sale.product.controller.manage;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hzs.common.core.annotation.Log;
import com.hzs.common.core.constant.CacheConstants;
import com.hzs.common.core.domain.R;
import com.hzs.common.core.enums.*;
import com.hzs.common.core.service.RedisService;
import com.hzs.common.core.utils.StringUtils;
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.sale.classify.BdSpecs;
import com.hzs.common.security.utils.SecurityUtils;
import com.hzs.sale.product.service.IBdSpecsService;
import com.hzs.system.base.ITransactionServiceApi;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Date;
import java.util.List;


/**
 * @Description:  规格controller
 * @Author: yuhui
 * @Time: 2022/8/29 10:59
 * @Classname: BdSpecsController
 * @PackageName: com.hzs.sale.product.controller.manage
 */
@Slf4j
@RestController
@RequestMapping("/manage/specs")
public class BdSpecsController extends BaseController {

    @DubboReference
    private ITransactionServiceApi transactionServiceApi;
    @Autowired
    private IBdSpecsService specsService;
    @Autowired
    private RedisService redisService;
    /**
     *  查询规格列表
     * @return
     */
    @GetMapping("list")
    @Log(module = EOperationModule.SPECS_LIST,business = EOperationBusiness.SPECIFICATIONS_CONFIG, method = EOperationMethod.SELECT)
    public TableDataInfo list(BdSpecs bdSpecs){

        QueryWrapper<BdSpecs> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(bdSpecs.getSpecsName())){
            queryWrapper.eq("SPECS_NAME",bdSpecs.getSpecsName());
        }
        startPage();
        List<BdSpecs> list = specsService.list(queryWrapper);
        return getDataTable(list);
    }

    /**
     * 新增规格
     * @param bdSpecs
     * @return
     */
    @PostMapping("save")
    @Log(module = EOperationModule.SPECS_LIST,business = EOperationBusiness.SPECIFICATIONS_CONFIG, method = EOperationMethod.INSERT)
    public AjaxResult save(@RequestBody BdSpecs bdSpecs){
        bdSpecs.setPkCreator(SecurityUtils.getUserId());
        bdSpecs.setCreationTime(new Date());
        bdSpecs.setPkCountry(SecurityUtils.getPkCountry());
        R<Integer> transaction = transactionServiceApi.createTransaction(SecurityUtils.getPkCountry(), ETransactionKey.SPEC, bdSpecs.getSpecsName(), EYesNo.NO, EYesNo.NO);
        bdSpecs.setPkTransactionKey(transaction.getData());
        specsService.save(bdSpecs);
        return AjaxResult.success();
    }

    /**
     * 修改规格
     * @param bdSpecs
     * @return
     */
    @PostMapping("update")
    @Log(module = EOperationModule.SPECS_LIST,business = EOperationBusiness.SPECIFICATIONS_CONFIG, method = EOperationMethod.UPDATE)
    public AjaxResult update(@RequestBody BdSpecs bdSpecs){
        BdSpecs specs = specsService.getById(bdSpecs.getPkId());
        bdSpecs.setPkModified(SecurityUtils.getUserId());
        bdSpecs.setModifiedTime(new Date());
        String redisKey = CacheConstants.BD_SPECS + bdSpecs.getPkId();
        transactionServiceApi.updateTransaction(specs.getPkCountry(),bdSpecs.getSpecsName(),specs.getPkTransactionKey(),true);
        redisService.deleteObject(redisKey);
        specsService.updateById(bdSpecs);
        return AjaxResult.success();
    }

    /**
     * 删除规格
     *
     * @param pkId
     * @return
     */
    @DeleteMapping("/{pkId}")
    @Log(module = EOperationModule.SPECS_LIST,business = EOperationBusiness.SPECIFICATIONS_CONFIG, method = EOperationMethod.DELETE)
    public AjaxResult delete(@PathVariable Integer pkId) {
        BdSpecs sp = specsService.getById(pkId);
        BdSpecs specs = new BdSpecs();
        specs.setPkId(pkId);
        specs.setDelFlag(EDelFlag.DELETE.getValue());
        specs.setModifiedTime(new Date());
        specs.setPkModified(SecurityUtils.getUserId());
        String redisKey = CacheConstants.BD_SPECS + pkId;
        redisService.deleteObject(redisKey);
        // 删除翻译
        transactionServiceApi.removeTransactionByPkId(Arrays.asList(sp.getPkTransactionKey().longValue()));
        return toAjax(specsService.updateById(specs));
    }

    /**
     * 查询规格详情
     * @param specsId
     * @return
     */
    @GetMapping("detail")
    public AjaxResult detail(Long specsId){
        return AjaxResult.success(specsService.getById(specsId));
    }


    /**
     * 修改规格状态
     * @return
     */
    @PostMapping("/updateEnable/{pkId}/{enable}")
    @Log(module = EOperationModule.SPECS_LIST,business = EOperationBusiness.SPECIFICATIONS_CONFIG, method = EOperationMethod.UPDATE)
    public AjaxResult updateEnable(@PathVariable Integer pkId,@PathVariable Integer enable){
        BdSpecs bdSpecs= new BdSpecs();
        bdSpecs.setEnableState(enable);
        bdSpecs.setPkId(pkId);
        bdSpecs.setPkModified(SecurityUtils.getUserId());
        bdSpecs.setModifiedTime(new Date());
        return toAjax(specsService.updateById(bdSpecs));
    }


    /**
     *  查询规格列表
     * @return
     */
    @GetMapping("find-all")
    @Log(module = EOperationModule.SPECS_LIST,business = EOperationBusiness.SPECIFICATIONS_CONFIG, method = EOperationMethod.SELECT)
    public AjaxResult findAll(){
        List<BdSpecs> list = specsService.list();
        return AjaxResult.success(list);
    }

}
