package com.dcsoft.system.consume.controller;

import com.dcsoft.common.core.utils.StringUtils;
import com.dcsoft.common.core.utils.poi.ExcelUtil;
import com.dcsoft.common.core.web.controller.BaseController;
import com.dcsoft.common.core.web.domain.AjaxResult;
import com.dcsoft.common.core.web.page.TableDataInfo;
import com.dcsoft.common.log.annotation.Log;
import com.dcsoft.common.log.enums.BusinessType;
import com.dcsoft.common.security.annotation.RequiresPermissions;
import com.dcsoft.system.consume.domain.*;
import com.dcsoft.system.consume.service.IConsumeRecordService;
import com.dcsoft.system.domain.SysPeople;
import com.dcsoft.system.service.ISysPeopleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 消费记录Controller
 *
 * @author nichun
 * @date 2023-09-08
 */
@RestController
@RequestMapping("/consumeRecord")
public class ConsumeRecordController extends BaseController
{
    @Autowired
    private IConsumeRecordService consumeRecordService;

    @Autowired
    private ISysPeopleService sysPeopleService;

    /**
     * 查询消费记录列表
     */
    @GetMapping("/list")
    public TableDataInfo list(ConsumeRecord consumeRecord)
    {
        startPage();
        List<ConsumeRecord> list = consumeRecordService.selectConsumeRecordList(consumeRecord);
        return getDataTable(list);
    }

    /**
     * 导出消费记录列表
     */
    @RequiresPermissions("system:consumeRecord:export")
    @Log(title = "消费记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ConsumeRecord consumeRecord)
    {
        List<ConsumeRecord> list = consumeRecordService.selectConsumeRecordList(consumeRecord);
        ExcelUtil<ConsumeRecord> util = new ExcelUtil<ConsumeRecord>(ConsumeRecord.class);
        util.exportExcel(response, list, "消费记录数据");
    }

    /**
     * 获取消费记录详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(consumeRecordService.selectConsumeRecordById(id));
    }

    /**
     * 新增消费记录
     */
    @RequiresPermissions("system:consumeRecord:add")
    @Log(title = "消费记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ConsumeRecord consumeRecord)
    {
        return toAjax(consumeRecordService.insertConsumeRecord(consumeRecord));
    }

    /**
     * 修改消费记录
     */
    @RequiresPermissions("system:consumeRecord:edit")
    @Log(title = "消费记录", businessType = BusinessType.UPDATE)



    public AjaxResult edit(@RequestBody ConsumeRecord consumeRecord)
    {
        return toAjax(consumeRecordService.updateConsumeRecord(consumeRecord));
    }

    /**
     * 删除消费记录
     */
    @RequiresPermissions("system:consumeRecord:remove")
    @Log(title = "消费记录", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(consumeRecordService.deleteConsumeRecordByIds(ids));
    }


    /**
     * 查询消费记录列表
     */
    @GetMapping("/businessList")
    public TableDataInfo businessList(BusinessSummary businessSummary)
    {
        //startPage();
        List<BusinessSummary> list = consumeRecordService.selectBusinessSummaryList(businessSummary);
        return getDataTable(list);
    }


    /**
     * 查询消费记录列表
     */
    @GetMapping("/consumeTimeList")
    public TableDataInfo consumeTimeList(ConsumeTime consumeTime)
    {
        //startPage();
        List<ConsumeTime> list = consumeRecordService.selectConsumeTimeList(consumeTime);
        return getDataTable(list);
    }


    /**
     * 查询消费记录列表
     */
    @GetMapping("/consumeSelfList")
    public TableDataInfo consumeSelfList(ConsumeSelf consumeSelf)
    {
        startPage();
        List<ConsumeSelf> list = consumeRecordService.selectConsumeSelfList(consumeSelf);
        return getDataTable(list);
    }


    /**
     * 导出商户营业列表
     */
    @Log(title = "商户营业", businessType = BusinessType.EXPORT)
    @PostMapping("/businessSummaryExport")
    public void businessSummaryExport(HttpServletResponse response, BusinessSummary businessSummary)
    {
        List<BusinessSummary> list = consumeRecordService.selectBusinessSummaryList(businessSummary);
        ExcelUtil<BusinessSummary> util = new ExcelUtil<BusinessSummary>(BusinessSummary.class);
        util.exportExcel(response, list, "商户营业数据");
    }

    /**
     * 导出餐段消费列表
     */
    @Log(title = "餐段消费", businessType = BusinessType.EXPORT)
    @PostMapping("/consumeTimeExport")
    public void consumeTimeExport(HttpServletResponse response, ConsumeTime consumeTime)
    {
        List<ConsumeTime> list = consumeRecordService.selectConsumeTimeList(consumeTime);
        ExcelUtil<ConsumeTime> util = new ExcelUtil<ConsumeTime>(ConsumeTime.class);
        util.exportExcel(response, list, "餐段消费数据");
    }

    /**
     * 导出个人消费列表
     */
    @Log(title = "个人消费", businessType = BusinessType.EXPORT)
    @PostMapping("/consumeSelfExport")
    public void consumeSelfExport(HttpServletResponse response, ConsumeSelf consumeSelf)
    {
        List<ConsumeSelf> list = consumeRecordService.selectConsumeSelfList(consumeSelf);
        ExcelUtil<ConsumeSelf> util = new ExcelUtil<ConsumeSelf>(ConsumeSelf.class);
        util.exportExcel(response, list, "个人消费数据");
    }

    /**
     * 查询消费记录列表
     */
    @GetMapping("/consumeBranchList")
    public TableDataInfo consumeBranchList(ConsumeBranch consumeBranch)
    {
        //startPage();
        List<ConsumeBranch> list = consumeRecordService.selectConsumeBranchList(consumeBranch);
        //添加其他消费
        /*ConsumeBranch  consumeBranchs =consumeRecordService.selectConsumeBranchByOther(consumeBranch);
        list.add(consumeBranchs);*/
        return getDataTable(list);
    }

    /**
     * 导出个人消费列表
     */
    @Log(title = "部门消费", businessType = BusinessType.EXPORT)
    @PostMapping("/consumeBranchExport")
    public void consumeBranchExport(HttpServletResponse response, ConsumeBranch consumeBranch)
    {
        List<ConsumeBranch> list = consumeRecordService.selectConsumeBranchList(consumeBranch);
        //添加其他消费
        /*ConsumeBranch  consumeBranchs =consumeRecordService.selectConsumeBranchByOther(consumeBranch);
        list.add(consumeBranchs);*/
        ExcelUtil<ConsumeBranch> util = new ExcelUtil<ConsumeBranch>(ConsumeBranch.class);
        util.exportExcel(response, list, "部门消费数据");
    }

    /**
     * 查询消费记录列表
     */
    @GetMapping("/consumeChart")
    public AjaxResult consumeChart()
    {
        return consumeRecordService.consumeChart();
    }

    /**
     * 查询消费记录列表
     */
    @GetMapping("/consumeBarChart")
    public AjaxResult consumeBarChart()
    {
        return consumeRecordService.consumeBarChart();
    }

    /**
     * 查询消费记录列表
     */
    @GetMapping("/dklineChart")
    public AjaxResult dklineChart()
    {
        return consumeRecordService.dklineChart();
    }

    @GetMapping("/pieChart")
    public AjaxResult pieChart()
    {
        return consumeRecordService.pieChart();
    }

    @PostMapping("/amendConsume")
    public void amendConsume(HttpServletResponse response) throws IOException
    {
        List<ConsumeRecord> list = consumeRecordService.selectAmendConsumeList();
        for (ConsumeRecord cr:list) {
            cr.setConsumeResult("1");
            cr.setRemark("更正重复订单");
            consumeRecordService.updateConsumeRecord(cr);
            if(StringUtils.isNotEmpty(cr.getAccountId())){
                SysPeople people=sysPeopleService.selectSysPeopleById(Long.parseLong(cr.getAccountId()));
                if(StringUtils.isNotEmpty(cr.getConsumeMoney())){
                    people.setBtje(people.getBtje()+Double.parseDouble(cr.getConsumeMoney()));
                    sysPeopleService.updateSysPeople(people);
                }
            }
        }
    }

    @PostMapping("/syncConsume")
    public void syncConsume(HttpServletResponse response) throws IOException
    {
         consumeRecordService.syncConsume();
    }

}
