package com.willalp.web.controller.integral;

import com.willalp.canteen.service.IIntegralSer2;
import com.willalp.common.annotation.Log;
import com.willalp.common.core.controller.BaseController;
import com.willalp.common.core.domain.AjaxResult;
import com.willalp.common.core.page.TableDataInfo;
import com.willalp.common.enums.BusinessType;
import com.willalp.common.enums.RechargeEnum;
import com.willalp.common.enums.RechargeTypeEnum;
import com.willalp.common.enums.ReturnEnum;
import com.willalp.common.utils.DateUtils;
import com.willalp.common.utils.poi.ExcelUtil;
import com.willalp.integral.domain.Integral;
import com.willalp.integral.domain.excelVo.IntegralVo;
import com.willalp.integral.domain.excelVo.IntegralVo2;
import com.willalp.integral.request.IntegralGroupQuery;
import com.willalp.integral.service.IIntegralService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.*;

/**
 * 积分管理Controller
 *
 * @author willalp
 * @date 2021-12-02
 */
@RestController
@RequestMapping("/pns/integral")
public class IntegralController extends BaseController {
    @Autowired
    private IIntegralService integralService;

    @Autowired
    private IIntegralSer2 ser2;

    /**
     * 查询积分管理列表
     */
    @PreAuthorize("@ss.hasPermi('pns:integral:list')")
    @GetMapping("/list")
    public TableDataInfo list(Integral integral) {
        startPage();
        List<Integral> list = integralService.selectIntegralList(integral);
        return getDataTable(list);
    }

    /**
     * 导出积分管理列表
     */
    @PreAuthorize("@ss.hasPermi('pns:integral:export')")
    @Log(title = "积分管理", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(Integral integral) {
        List<Integral> list = integralService.selectIntegralList(integral);
        ExcelUtil<Integral> util = new ExcelUtil<Integral>(Integral.class);
        return util.exportExcel(list, "积分管理数据");
    }

    @GetMapping("/importTemplate")
    public AjaxResult importTemplate() {
        ExcelUtil<IntegralVo> util = new ExcelUtil<IntegralVo>(IntegralVo.class);
        List<IntegralVo> list = integralService.rechargeTemplate();
        return util.exportExcel(list, "积分充值数据");
    }

    /**
     * 获取积分管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('pns:integral:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return AjaxResult.success(integralService.getById(id));
    }

    /**
     * 新增积分管理
     */
    //@PreAuthorize("@ss.hasPermi('pns:integral:add')")
    @Log(title = "积分管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Integral integral) {
        ser2.saveInfo(integral);
        return AjaxResult.success();
    }

    /**
     * 修改积分管理
     */
    @PreAuthorize("@ss.hasPermi('pns:integral:edit')")
    @Log(title = "积分管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Integral integral) {
        integral.setUpdateTime(DateUtils.getNowDate());
        return toAjax(integralService.updateById(integral));
    }

    /**
     * 删除积分管理
     */
    @PreAuthorize("@ss.hasPermi('pns:integral:remove')")
    @Log(title = "积分管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(integralService.removeByIds(Arrays.asList(ids)));
    }

    @PostMapping("/saveBacth")
    public AjaxResult saveBacth(@RequestBody List<Integral> list) {
        for (Integral integral : list) {
            integral.setMoneyType(RechargeTypeEnum.collective.getCode());
            ser2.saveInfo(integral);
        }
        return AjaxResult.success();
    }

    @PreAuthorize("@ss.hasPermi('pns:integral:returnPoints')")
    @PostMapping("/doReturnPoints")
    public AjaxResult returnPoints(@RequestBody List<IntegralGroupQuery> list) {
        integralService.returnPoints(list);
        return AjaxResult.success();
    }

    @Log(title = "积分管理", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file) throws Exception {
        ExcelUtil<Integral> util = new ExcelUtil<Integral>(Integral.class);
        List<Integral> list = util.importExcel(file.getInputStream());
        return AjaxResult.success(integralService.importData(list));
    }

    @PreAuthorize("@ss.hasPermi('pns:integral:groupList')")
    @GetMapping("/groupList")
    public TableDataInfo groupList(IntegralGroupQuery integralGroupQuery) {
        startPage();
        List<IntegralVo2> list = integralService.queryIntegralSum(integralGroupQuery);
        return getDataTable(list);
    }

    /**
     * 导出所有用户余额
     * */
    @Log(title = "积分管理", businessType = BusinessType.EXPORT)
    @GetMapping("/exportGroupList")
    public AjaxResult exportGroupList(IntegralGroupQuery integralGroupQuery) {
        List<IntegralVo2> list = integralService.queryIntegralSum(integralGroupQuery);
        ExcelUtil<IntegralVo2> util = new ExcelUtil<>(IntegralVo2.class);
        return util.exportExcel(list, "所有用户余额数据");
    }


    @GetMapping("/getIntegralType")
    public AjaxResult getIntegralType() {
        Map result = new HashMap();
        //充值
        List<Map> recharges = new ArrayList<>();
        RechargeEnum[] rechargeEnums = RechargeEnum.values();
        for (int i = 0; i < rechargeEnums.length; i++) {
            Map temp = new HashMap();
            temp.put("label", rechargeEnums[i].getLabel());
            temp.put("value", rechargeEnums[i].getValue());
            recharges.add(temp);
        }
        //退还
        List<Map> returns = new ArrayList<>();
        ReturnEnum[] returnEnums = ReturnEnum.values();
        for (int i = 0; i < returnEnums.length; i++) {
            Map temp = new HashMap();
            temp.put("label", returnEnums[i].getLabel());
            temp.put("value", returnEnums[i].getValue());
            returns.add(temp);
        }
        result.put("recharge", recharges);
        result.put("return", returns);
        return AjaxResult.success("成功", result);
    }
}
