package com.storlead.sales.controller.systemrule;


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.storlead.framework.common.constant.CommonConstant;
import com.storlead.framework.common.result.Result;
import com.storlead.sales.pojo.dto.systemrule.CurrencySettingsAddDTO;
import com.storlead.sales.pojo.dto.systemrule.CurrencySettingsDTO;
import com.storlead.sales.pojo.entity.systemrule.CurrencySettingsEntity;
import com.storlead.sales.pojo.vo.systemrule.CurrencySettingsVO;
import com.storlead.sales.service.systemrule.CurrencySettingsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.beans.BeanUtils;
import javax.annotation.Resource;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 币种设置表 前端控制器
 * </p>
 *
 * @author YPZ
 * @since 2024-04-08
 */
@RestController
@RequestMapping("/currency-settings-entity")
@Api(tags = "销售: 币种设置相关接口")
public class CurrencySettingsController {

    @Resource
    private CurrencySettingsService currencySettingsService;

    @PostMapping(value = "/list")
    @ApiOperation(value = "币种设置查询-列表", notes = "币种设置查询-列表")
    @ApiResponses({
            @ApiResponse(code = 200, message = "", response = CurrencySettingsDTO.class)
    })
    public Result<?> list(@RequestBody CurrencySettingsDTO currencySettingsDTO) {

        Page<CurrencySettingsEntity> page = new Page<>(currencySettingsDTO.getPageIndex(), currencySettingsDTO.getPageSize());

        LambdaQueryWrapper<CurrencySettingsEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(CurrencySettingsEntity::getIsDelete, CommonConstant.DEL_FLAG_0);
        wrapper.orderByDesc(CurrencySettingsEntity::getHasTop);
        wrapper.orderByDesc(CurrencySettingsEntity::getTopTime);
        IPage<CurrencySettingsEntity> pageList = currencySettingsService.page(page, wrapper);
        List<CurrencySettingsVO> list = new ArrayList<>();
        if (!ObjectUtils.isEmpty(pageList)){
            pageList.getRecords().forEach(item -> {
                CurrencySettingsVO currencySettingsVO = new CurrencySettingsVO();
                BeanUtils.copyProperties(item, currencySettingsVO);
                list.add(currencySettingsVO);
            });
        }
        IPage<CurrencySettingsVO> pageVO = new Page<>();
        pageVO.setRecords(list);
        pageVO.setTotal(pageList.getTotal());
        pageVO.setPages(pageList.getPages());
        pageVO.setCurrent(pageList.getCurrent());

        return Result.result(pageVO);

    }

    @PostMapping(value = "/add")
    @ApiOperation(value = "币种设置-新增/编辑币种", notes = "币种设置-新增/编辑币种")
    @Transactional(rollbackFor = Throwable.class)
    public Result<?> add(@RequestBody CurrencySettingsAddDTO currencySettingsDTO) {
        if (ObjectUtils.isEmpty(currencySettingsDTO)){
            return Result.error("参数不能为空");
        }

        CurrencySettingsEntity currencySettingsEntity = new CurrencySettingsEntity();
        BeanUtils.copyProperties(currencySettingsDTO, currencySettingsEntity);
        if (!ObjectUtils.isEmpty(currencySettingsEntity.getHasTop()) && currencySettingsEntity.getHasTop()){
            currencySettingsEntity.setTopTime(LocalDateTime.now());
        }
        boolean isOk = currencySettingsService.saveOrUpdate(currencySettingsEntity);
       if (isOk){
           return Result.result("操作成功！");
       }
       return Result.error("操作失败!");
    }

    @PostMapping("/delete")
    @ApiOperation(value = "币种设置-删除", notes = "币种设置-删除")
    public Result delete(Long id) {
        if(Objects.isNull(id)) {
            return Result.error("参数错误");
        }
        currencySettingsService.lgDelete(id);
        return Result.ok();
    }
}
