package com.jzdsh.auto.app.controller.admin;

import com.jfinal.aop.Before;
import com.jfinal.aop.Inject;
import com.jfinal.core.Controller;
import com.jfinal.kit.FileKit;
import com.jfinal.kit.PathKit;
import com.jfinal.kit.Ret;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.upload.UploadFile;
import com.jzdsh.auto.app.controller.admin.mvalidator.AdminBsTypeValidator;
import com.jzdsh.auto.app.controller.admin.mvalidator.AdminOwnerValidator;
import com.jzdsh.auto.biz.BsTypeService;
import com.jzdsh.auto.biz.OwnerService;
import com.jzdsh.auto.model.BsType;
import com.jzdsh.auto.model.Owner;
import com.jzdsh.util.CommonCons;

import java.io.File;
import java.util.Date;

/**
 * @ProjectName: auto
 * @Package: com.jzdsh.auto.app.controller.admin
 * @ClassName: AdminOwnerController
 * @Author: Jzdsh
 * @Description:
 * @Date: 2020/3/26 18:51
 * @Version: 1.0
 */
public class AdminOwnerController extends Controller {


    @Inject
    OwnerService ownerService;
    @Inject
    BsTypeService bsTypeService;

    public void index( ) {
        setAttr("ownerPage", ownerService.findAll(getInt("pageNum", 1), 10));
    }


    public void add( ) {
        setAttr("type", bsTypeService.getByStatus(3));
        setAttr("owner", new Owner().setTypeId(0));
        render("add_edit.html");
    }

    @Before(AdminOwnerValidator.class)
    public void save( ) {
        UploadFile file = getFile("owner.img", "/owner/");

        Owner bean = getBean(Owner.class);
        if (file != null) {
            bean.setImg(CommonCons.PROJECT_NAME + "/upload/owner/" + file.getFileName( ));
        }
        boolean save = bean.set("releaseTime", new Date( )).set("createDate", new Date( )).set("updateDate", new Date( )).set("pv", 0).set("isCollection", 0).save( );
        if (save) {
            renderJson(Ret.ok("msg", "创建成功!"));
        } else {
            renderJson(Ret.fail("msg", "创建失败!"));
        }
    }

    public void edit( ) {
        setAttr("type", bsTypeService.getByStatus(3));
        setAttr("owner", Owner.dao.findById(getInt( )));
        render("add_edit.html");
    }

    @Before(AdminOwnerValidator.class)
    public void update( ) {

        UploadFile file = getFile("owner.img", "/owner/");

        Owner bean = getBean(Owner.class);
        if (file != null) {
            bean.setImg(CommonCons.PROJECT_NAME + "/upload/owner/" + file.getFileName( ));
        }
        boolean update = bean.set("updateDate", new Date( )).removeNullValueAttrs().update( );
        if (update) {
            renderJson(Ret.ok("msg", "更新成功!"));
        } else {
            renderJson(Ret.fail("msg", "更新失败!"));
        }
    }

    public void delete( ) {
        //删除图片
        Owner byId = Owner.dao.findById(getInt( ));
        if(byId != null && byId.getImg().length() > 0){
            String webRootPath = PathKit.getWebRootPath( );
            String s = webRootPath + byId.getImg( ).replace("/nvrnetwork","");
            FileKit.delete(new File(s));
        }
        //删除信息
        boolean b = Owner.dao.deleteById(getInt( ));
        if (b) {
            renderJson(Ret.ok("msg", "删除成功!"));
        } else {
            renderJson(Ret.fail("msg", "删除失败!"));
        }
    }

    /**
     * 快速更改推荐信息
     */
    public void changeRecommend_f(){
        Owner.dao.findById(getInt("id")).set("isRecommend",0).removeNullValueAttrs().update();
        setAttr("ownerPage", ownerService.findAll(getInt("pageNum", 1), 10));
        render("index.html");
    }

    public void changeRecommend_t(){
        Owner.dao.findById(getInt("id")).set("isRecommend",1).removeNullValueAttrs().update();
        setAttr("ownerPage", ownerService.findAll(getInt("pageNum", 1), 10));
        render("index.html");
    }

    /**
     * 快速更改是否置顶
     */
    public void changeTop_f(){
        Owner.dao.findById(getInt("id")).set("isTop",0).removeNullValueAttrs().update();
        setAttr("ownerPage", ownerService.findAll(getInt("pageNum", 1), 10));
        render("index.html");
    }

    public void changeTop_t(){
        Owner.dao.findById(getInt("id")).set("isTop",1).removeNullValueAttrs().update();
        setAttr("ownerPage", ownerService.findAll(getInt("pageNum", 1), 10));
        render("index.html");
    }


}
